Split a list of results from devcon in python

69 views Asked by At

I'm writing a python script where I'm trying to gather a list device connected via USB with the goal of removing the USB ports so I can readd them guaranteeing a specific cable is COM3. I have a batch file that does this, however I need to add more functionality than the batch file can handle.

list = []
#This section finds all connected USB ports and works
ports = subprocess.run("devcon FindAll =ports", stdout=subprocess.PIPE).stdout.splitlines()
for x in ports:
     list.append(x.decode('utf-8'))
#This section splits the results from above and places them in a list
print(list[0])
cmd_0 = str('devcon remove @\\" + list[0]) 
list2 = [cmd_0.split(" ", 1 [1] for item in list]
print(cmd_0)

when I run the code I get the following result:

**devcon remove @usb\ACPI\PNP0501\0**                                              : Communications Port (COM1)

What I can't figure out is how to get rid of everything after the bold section.

Thanks in advance for the help.

I have tried various methods for splitting the cmd_0 output but can't find the correct method.

0

There are 0 answers