In Python, how to separate local hard drives from the network and floppy disks in Windows?

I have been looking for this information for a while, and I have several ways to get a list of local drives under Windows. Here are two examples:

print win32api.GetLogicalDriveStrings().split("\x00") 

and

 def getDriveLetters(self): self.drvs = [] n_drives = win32api.GetLogicalDrives() for i in range(0,25): #check all drive letters j = 2**i # bitmask for each letter if n_drives & j > 0: self.drvs.append(chr(65+i)+":/") print self.drvs 

What I cannot find is a way to separate floppy disks (A :), USB-drives (G :), CD-ROMs (E :) and network drives (P :) from local hard drives (C :, D: )

If the same letters were assigned to all of them, it would be easy, but I am writing this script to control the local space on the hard drive in a network of computers with different configurations.

Any help would be appreciated! Thanks.

+7
source share
2 answers

You can try win32 GetDriveType .

 import win32file >>> win32file.GetDriveType("C:/") == win32file.DRIVE_FIXED ##hardrive True >>> win32file.GetDriveType("Z:/") == win32file.DRIVE_FIXED ##network False >>> win32file.GetDriveType("D:/") == win32file.DRIVE_FIXED ##cd-rom False 
+9
source

Thanks for your post - helped me with a ruby ​​port. The getDriveLetters method returns hash (dict): drive letter string, drive type string.

 require 'Win32API' GetLogicalDrives = Win32API.new('kernel32', 'GetLogicalDrives', 'V', 'L') GetDriveType = Win32API.new('kernel32', 'GetDriveType', 'P', 'I') def GetDriveType(path) GetDriveType.call(path) end def GetLogicalDrives() GetLogicalDrives.call() end def getDriveLetters drivetype = { 0 => 'DRIVE_UNKNOWN', 1 => 'DRIVE_NO_ROOT_DIR', 2 => 'DRIVE_REMOVABLE', 3 => 'DRIVE_FIXED', 4 => 'DRIVE_REMOTE', 5 => 'DRIVE_CDROM', 6 => 'DRIVE_RAMDISK' } drvs = [] n_drives = GetLogicalDrives() for i in 0..25 do #check all drive letters j = 2**i # bitmask for each letter if n_drives & j > 0 then drive = (65+i).chr + ":/" drvs += [drive => drivetype[GetDriveType(drive)]] end end return drvs end puts getDriveLetters 
0
source

All Articles