简体   繁体   English

使用Python查询连接到Windows的USB设备

[英]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: 典型流量:

  • Get all Locations 获取所有位置
  • Pick up that a new location F:\\ is available 确认有一个新的位置F:\\
  • Copy a file over using shutil.copy2 使用shutil.copy2复制文件

So how would I identify which port(?) has just been plugged in F , G , H , etc? 那么,如何识别刚刚插入FGH等的端口(?)? 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.

相关问题 在 Python 中查询连接的 USB 设备信息的简单方法? - Simple way to query connected USB devices info in Python? 在 Windows 中使用 python 3 枚举连接到我的路由器的设备 - Enumerate devices connected to my router using python 3 in Windows 如何在 Python 中运行 PowerShell cmdlet 以获取已连接 USB 设备的列表? - How to run a PowerShell cmdlet in Python to get a list of connected USB devices? 我们可以在 python 中检测到连接到 USB 集线器的设备吗 - Can we detect devices connected to USB hub in python 如何使用 python 或 ZDFFF0A7FA1A55C8C1A4966C19F6DA425 从 windows 连接 USB 设备列表 - How to get connected USB device list from windows by using python or cmd Python - 在 windows 中使用 pyUsb 获取 USB 设备的序列号 - Python - getting SerialNumber of usb-devices with pyUsb in windows 从使用 python 通过 usb 连接的摄像机获取实时 Stream - Get Live Stream from a Camera connected via usb using python 在 python 中使用 sendevent 到 usb 由于 su 访问连接手机速度慢 - Using sendevent in python to usb connected phone slow due to su access 使用 Python 3 (Windows 10) 弹出设备/USB - Eject Device/USB using Python 3 (Windows 10) 用于 wifi 连接设备的 python 程序 - python program for wifi connected devices
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM