最後我用了一個更簡單的方法。
我使用Python附帶的winreg
模塊來訪問Windows註冊表。 HKEY_LOCAL_MACHINE\SYSTEM\MountedDevices
跟蹤所有已安裝的設備(當前是否連接)。因此,我從那裏獲取所有設備信息並檢查設備當前是否已連接,我只需簡單os.path.exists
設備的存儲號(即G:
)。存儲信可以從密鑰MountedDevices
中獲得。
實施例:
# Make it work for Python2 and Python3
if sys.version_info[0]<3:
from _winreg import *
else:
from winreg import *
# Get DOS devices (connected or not)
def get_dos_devices():
ddevs=[dev for dev in get_mounted_devices() if 'DosDevices' in dev[0]]
return [(d[0], regbin2str(d[1])) for d in ddevs]
# Get all mounted devices (connected or not)
def get_mounted_devices():
devs=[]
mounts=OpenKey(HKEY_LOCAL_MACHINE, 'SYSTEM\MountedDevices')
for i in range(QueryInfoKey(mounts)[1]):
devs+=[EnumValue(mounts, i)]
return devs
# Decode registry binary to readable string
def regbin2str(bin):
str=''
for i in range(0, len(bin), 2):
if bin[i]<128:
str+=chr(bin[i])
return str
然後,只需運行:
get_dos_devices()
Winusb.ddl:http://msdn.microsoft.com/en-us/library/windows/hardware/ff540046%28v = vs.85%29.aspx也相關:http://stackoverflow.com/questions/12926923/winusb-dll-and-python-specifically-winusb-initialize – jmunsch 2014-11-03 17:16:47