7

How can I retrieve the items plugged into the computer through USB using python? I've searched around on here and found some old examples which don't appear to work anymore as they are over 5 years old.

I would like an example where I can list the names seen in the image below which is a screenshot of the Device Manager.

enter image description here

The code below lists all the USB devices but none of the data returns the Names seen in the image, why is that?

import win32com.client
import pprint

wmi = win32com.client.GetObject ("winmgmts:")
for usb in wmi.InstancesOf ("Win32_USBHub"):
    # print usb.DeviceID
    # pprint.pprint (dir(usb))
    # pprint.pprint (vars(usb))
    # print usb.__dict__
    print ('Device ID:', usb.DeviceID)
    print ('Name:', usb.name)
    print ('System Name:', usb.SystemName)
    print ('Caption:', usb.Caption)
    print ('Caption:', usb.Caption)
    print ('ClassCode:', usb.ClassCode)
    print ('CreationClassName:', usb.CreationClassName)
    print ('CurrentConfigValue:', usb.CurrentConfigValue)
    print ('Description:', usb.Description)
    print ('PNPDeviceID:', usb.PNPDeviceID)
    print ('Status:', usb.Status)
    print ('\n')
3
  • Could you show what your code gets for one of the entries? I would not be surprised if the nice labels were part of Windows Explorer... Windows philosophy is that an end user should not deal with system data. Commented Jan 31, 2017 at 15:09
  • I didn't know Python can consume WMI. There may be some WMI API that you can use for this, that returns something like what you see in the Device Manager Commented Jan 31, 2017 at 15:16
  • Your title asks about USB, but the text of your comment seems to imply you are interested in COM devices only. Are you interested in ALL USB connections, or just the Serial ports? Commented Apr 5, 2022 at 18:08

1 Answer 1

1

I would install pyserial

py -m pip install pyserial

and use serial.tools.list_ports. You can execute it as a module:

py -m serial.tools.list_ports

or incorporate the module and its corresponding methods into your code base:

import serial
from serial.tools import list_ports

if __name__ == "__main__":
    for port in list_ports.comports():
        if "USB" in port.hwid:
            print(f"Name: {port.name}")
            print(f"Description: {port.description}")
            print(f"Location: {port.location}")
            print(f"Product: {port.product}")
            print(f"Manufacturer: {port.manufacturer}")
            print(f"ID: {port.pid}")

More information can be found here:

pyserial docs >> tools

Sign up to request clarification or add additional context in comments.

1 Comment

Your code will enumerate USB COM ports but the OP question title asks about all USB devices, though the text screenshot only shows COM ports from the device manager. I think the title should be more specific if only COM ports are wanted. In that case, you answer is perfect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.