Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in Python, how to separate Local Hard Drives from Network and Floppy in Windows?

Tags:

python

winapi

I've been looking for this info for awhile, and I have a number of ways to retrieve 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 can't seem to find is a way to separate the floppies (A:), usb drives (G:), CD drives (E:), and network drives (P:) from the Local hard drives (C:, D:)

If they all were assigned the same letters it would be easy, but I'm writing this script to monitor local hard disk space across a network of computers with different configurations.

Any help would be appreciated! Thanks.

like image 500
Nathan Garabedian Avatar asked Dec 20 '10 16:12

Nathan Garabedian


1 Answers

You can try the win32 GetDriveType function.

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
like image 59
Mark Avatar answered Oct 03 '22 09:10

Mark