I have read Is there a way to list all the available Windows' drives? and Cross platform way to list disk drives on Linux, Windows and Mac using Python? and methods like:
import win32api
print(win32api.GetLogicalDriveStrings().split('\000'))
but how to limit this list to external USB storage devices only? (USB HDD, USB SSD, USB flash drive, etc.)
PS: is it possible with very few dependencies? (maybe just ctypes
or win32api
)
Here is a solution with only ctypes
and no third-party module to pip install
, inspired by Bhargav's solution:
import ctypes, string
bitmask = ctypes.windll.kernel32.GetLogicalDrives()
drives = [letter for i, letter in enumerate(string.ascii_uppercase) if bitmask & (1 << i)]
ext_drives = [letter for letter in drives if ctypes.windll.kernel32.GetDriveTypeW(letter + ':') == 2] # DRIVE_REMOVABLE = 2
print(ext_drives)