[英]Query USB devices connected to Windows using Python
There are a lot of answers for C#, C++, python, ubuntu, etc but this is preferred for Windows and python (or else simply call subprocess through python) 对于C#,C ++,python,ubuntu等,有很多答案,但这是Windows和python的首选(或者直接通过python调用子进程)
I have this code that worked on Ubuntu, but now I want to switch to windows: 我有可以在Ubuntu上运行的这段代码,但是现在我想切换到Windows:
rpistr = "ls /media/pi > usbs.txt"
p=subprocess.Popen(rpistr,shell=True, preexec_fn=os.setsid)
array = []
with open("usbs.txt", "r") as f:
for line in f:
array.append(line.strip())
I'd like to check the ports connected, and list the directory of the USB. 我想检查连接的端口,并列出USB的目录。
For example, let's say I have C:\\
and D:\\
, but now I plug in a new USB, which will be F:\\
as an example - I want to find out that the path F:\\
is now available to me and be able to copy a file over. 例如,假设我有
C:\\
和D:\\
,但是现在我插入了一个新的USB,以F:\\
为例-我想找出路径F:\\
现在可供我使用并能够复制文件。
Typical flow: 典型流量:
F:\\
is available F:\\
shutil.copy2
shutil.copy2
复制文件 So how would I identify which port(?) has just been plugged in F
, G
, H
, etc? 那么,如何识别刚刚插入
F
, G
, H
等的端口(?)? I plan on running this on an infinite loop, that's why I need to identify when and where the new USB is plugged. 我打算无限循环地运行它,这就是为什么我需要确定何时以及在何处插入新USB。
Didn't realize they were called Drives, figured it out and found the right answer. 没意识到他们被称为驱动器,想通了,找到了正确的答案。
You can simply use: 您可以简单地使用:
import win32com.client
drives = win32api.GetLogicalDriveStrings()
drives = drives.split('\000')[:-1]
print(drives)
and it will output the answer as such: 它会这样输出答案:
['C:\\', 'D:\\', 'E:\\', 'F:\\']
Just calling on drives[x]
, x
being an index, will return the example drive as X:\\
which works perfectly with shutil
. 只需调用
drives[x]
( x
是索引),该示例驱动器将以X:\\
返回,这与shutil
完美配合。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.