每当插入闪存驱动器时,我都能够检测到。但是,(听起来很傻)无法检测是否插入了多个 USB。此外,我在编写代码以从中读取数据时遇到困难(它们)。有人可以帮忙吗?谢谢,提前:) PS我的系统是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."