无论何时插入闪存驱动器,我都能够检测到。但是,(听起来很傻)无法检测是否插入了多个USB。 此外,我很难编写代码来读取它的数据(他们)。 有人可以帮忙吗? 提前致谢 :) 附:我的系统是Windows 8 64位 整个过程就是检测我将从传感器中拾取的信号。
这是检测USB的代码
import string
from ctypes import windll
import time
import os
def get_drives():
drives = []
bitmask = windll.kernel32.GetLogicalDrives()
for letter in string.uppercase:
if bitmask & 1:
drives.append(letter)
bitmask >>= 1
return drives
if __name__ == '__main__':
before = set(get_drives())
pause = raw_input("Please insert the USB device, then press ENTER")
print ('Please wait...')
time.sleep(5)
after = set(get_drives())
drives = after - before
delta = len(drives)
if (delta):
for drive in drives:
if os.system("cd " + drive + ":") == 0:
newly_mounted = drive
print "There were %d drives added: %s. Newly mounted drive letter is %s" % (delta, drives, newly_mounted)
else:
print "Sorry, I couldn't find any newly mounted drives."
答案 0 :(得分:0)
我在我的电脑上尝试了你的代码。它工作得很好。我的系统是win7 64bit pro。