Retrieve list of USB items using Python

2024/10/4 17:22:10

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 pprintwmi = 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')
Answer

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_portsif __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

https://en.xdnf.cn/q/70583.html

Related Q&A

History across ipdb sessions

This question has been asked before, but I couldnt find a good answer. So, I am trying to ask again.I would like my ipdb to remember commands across sessions. Right now, it can pull up commands execute…

Python Distributed Computing (works)

Im using an old thread to post new code which attempts to solve the same problem. What constitutes a secure pickle? this?sock.pyfrom socket import socket from socket import AF_INET from socket import…

Django - Stream request from external site as received

How can Django be used to fetch data from an external API, triggered by a user request, and stream it directly back in the request cycle without (or with progressive/minimal) memory usage?BackgroundAs…

django rest framework - always INSERTs, never UPDATES

I want to be able to UPDATE a user record by POST. However, the id is always NULL. Even if I pass the id it seems to be ignoredView Code:JSON POSTED:{"id": 1, "name": "Craig Ch…

Copy fields from one instance to another in Django

I have the following code which takes an existing instance and copies, or archives it, in another model and then deletes it replacing it with the draft copy. Current Codedef archive_calc(self, rev_num,…

Python selenium get Developer Tools →Network→Media logs

I am trying to programmatically do something that necessarily involves getting the "developer tools"→network→media logs. I will spare you the details, long story short, I need to visit thou…

deep copy nested iterable (or improved itertools.tee for iterable of iterables)

PrefaceI have a test where Im working with nested iterables (by nested iterable I mean iterable with only iterables as elements). As a test cascade considerfrom itertools import tee from typing import …

ImportError: No module named cv2.cv

python 3.5 and windows 10I installed open cv using this command :pip install opencv_python-3.1.0-cp35-cp35m-win_amd64.whlThis command in python works fine :import cv2But when i want to import cv2.cv :i…

Why arent persistent connections supported by URLLib2?

After scanning the urllib2 source, it seems that connections are automatically closed even if you do specify keep-alive. Why is this?As it is now I just use httplib for my persistent connections... bu…

How to find accented characters in a string in Python?

I have a file with sentences, some of which are in Spanish and contain accented letters (e.g. ) or special characters (e.g. ). I have to be able to search for these characters in the sentence so I can…