Reputation: 123
I'm need to control a certain USB port in windows. I am working on a projet to automate Testcases.
Problem: In some tests you need to plug-in and out the USB pendrive to run the test 200 times for example.
Solution: Write a python program and integrate it in the tests to emulate this process of plug-in and out of a USB.
i have found this code below but "subprocess" doesn't have run reference
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
Write a python program and integrate it in the tests to emulate this process of plug-in and out of a USB.
Upvotes: 3
Views: 7429
Reputation: 123
Well this is the solution i was able to use and it is working fine with me.
import subprocess
# find all devices command
Find_command = 'C:/Windows/SysWOW64/devcon.exe find *'
#list HW ids of devices
hwIds_command = 'C:/devcon.exe hwids *'
#Enable
Enable_command = 'C:/devcon.exe enable *PID_1016'
#Disable
Disable_command = 'C:/devcon.exe disable *mouse*'
#Find Device
Find_SpecificDevice_command = 'C:/devcon.exe find *PID_1016 '
find= True
try:
if find:
result = subprocess.check_output(Find_SpecificDevice_command,shell=True ,stderr=subprocess.STDOUT)
print(result)
except subprocess.CalledProcessError as e:
raise RuntimeError("command '{}' return with error (code {}): {}".format(e.cmd, e.returncode, e.output))
If you have any questions regarding the solution feel free to ask.
Upvotes: 1
Reputation: 74685
subprocess.run
is the replacement for subprocess.call
that has different arguments so they had to give it a new name. But as in this case the code requires none of the new features you can just replace subprocess.run
with subprocess.call
.
And in the case of subprocess.run
with the option capture_output
that can be replaced with subprocess.check_output
.
Upvotes: 1