我需要在 Windows 中控制某个 USB 端口。我正在开发一个自动化测试用例的项目。
问题:例如,在某些测试中,您需要插入和拔出 USB 随身碟以运行测试 200 次。
解决方案:写一个python程序,集成到测试中,模拟U盘的插拔过程。
我在下面找到了这段代码,但是“子进程”没有运行引用
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
编写一个python程序并集成到测试中,模拟USB插拔过程。
请您参考如下方法:
subprocess.run 是 subprocess.call 的替代品,它具有不同的参数,因此他们必须给它一个新名称。但在这种情况下,代码不需要任何新功能,您只需将 subprocess.run 替换为 subprocess.call。
在 subprocess.run 的情况下,选项 capture_output 可以替换为 subprocess.check_output。
