Find Drive Types


What drives do I have and what type are they? The slightly awkward GetLogicalDriveStrings returns a string of zero-delimited drive letters with a final extra delimiter. (Which explains the "if drive" at the end of the generator expression below.) The GetDriveType function then takes the drive letter, colon, and backslash, exactly as returned by splitting the logical drive string, and returns an index into a lookup table which I've cut-and-pasted here from MSDN.

See also the same thing using WMI

import win32api
import win32file

# cut-and-pasted from MSDN
0 	Unknown
1 	No Root Directory
2 	Removable Disk
3 	Local Disk
4 	Network Drive
5 	Compact Disc
6 	RAM Disk
drive_types = dict((int (i), j) for (i, j) in (l.split ("\t") for l in DRIVE_TYPES.splitlines () if l))

drives = (drive for drive in win32api.GetLogicalDriveStrings ().split ("\000") if drive)
for drive in drives:
  print drive, "=>", drive_types[win32file.GetDriveType (drive)]