[英]how to detect multiple USB ports and read data from USB using python
I have been able to detect whenever a flash drive is inserted. 我已经能够检测到何时插入了闪存驱动器。 However, (silly as it sounds) having trouble to detect if multiple USB be inserted. 但是,(听起来很傻)难以检测是否插入了多个USB。 Also, i am having difficulty in writing the code to read to data from it(them). 另外,我在编写代码以读取其中的数据时遇到了困难。 Can someone please help? 有人可以帮忙吗? Thanks, in advance :) PS my system is windows 8 64 bit The whole thing is about detecting signals that i will pick from sensors. 谢谢,提前:) PS我的系统是Windows 8 64位整个过程是关于检测我将从传感器中拾取的信号。
here is the code to detect USB 这是检测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."
我在计算机上尝试了您的代码。它运行正常。我的系统是win7 64bit pro。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.