[英]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. 我一直在寻找这个信息,我有很多方法可以在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:) 我似乎无法找到一种方法来从本地硬盘驱动器(C:,分离)软盘(A :),USB驱动器(G :),CD驱动器(E :)和网络驱动器(P :) 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. 您可以尝试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
Thank you for your post - helped me with a ruby port. 谢谢您的帖子 - 帮我一个红宝石端口。 Method getDriveLetters returns hash (dict): drive letter string, drive type string. 方法getDriveLetters返回hash(dict):驱动器盘符串,驱动器类型字符串。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.