首页 > 解决方案 > 如何使用 python 在 Windows 中禁用/启用特定的 USB 端口?

问题描述

我想在图形窗口中创建一个切换开关,可以使用 python 禁用/启用 Windows 中的特定 USB 端口,我可以使用哪个外部命令或库?

标签: pythonwindows

解决方案


使用devcon.exe

import subprocess
# Fetches the list of all usb devices:
result = subprocess.run(['devcon', 'hwids', '=usb'], 
    capture_output=True, text=True)

# ... add code to parse the result and get the hwid of the device you want ...

subprocess.run(['devcon', 'disable', parsed_hwid]) # to disable
subprocess.run(['devcon', 'enable', parsed_hwid]) # to enable

推荐阅读