简体   繁体   中英

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

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.

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

Thank you for your post - helped me with a ruby port. Method getDriveLetters 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM