问题
I need to get connected USB device list from windows by using python or cmd.
for python i'm trying this.
import win32com.client
def get_usb_device():
try:
usb_list = []
wmi = win32com.client.GetObject("winmgmts:")
for usb in wmi.InstancesOf("Win32_USBHub"):
print(usb.DeviceID)
print(usb.description)
usb_list.append(usb.description)
print(usb_list)
return usb_list
except Exception as error:
print('error', error)
get_usb_device()
as a result i get this:
['USB Root Hub (USB 3.0)', 'USB Composite Device', 'USB Composite Device']
but i don't get a meaning full name.
and for cmd i'm trying this also:
wmic path CIM_LogicalDevice where "Description like 'USB%'" get /value
and again i don't get any meaning full name for connected usb devices.
when I'm connect a mouse, keyboard, pen drive or printer through the usb i want this kind of name. like 'a4tech mouse' or even if i get 'mouse' only that's also be fine. This type of name appears in the device section of the settings of windows 10. but i get 'USB Root Hub (USB 3.0)', 'USB Composite Device', which means nothing actually. Is it possible with python?
If any one know this answer please help. Its very important for me.
回答1:
Not sure if it's what you are looking for, but using Python 3 on Windows 10 with pywin32, you could use this to get all your drive letters and types:
import os
import win32api
import win32file
os.system("cls")
drive_types = {
win32file.DRIVE_UNKNOWN : "Unknown\nDrive type can't be determined.",
win32file.DRIVE_REMOVABLE : "Removable\nDrive has removable media. This includes all floppy drives and many other varieties of storage devices.",
win32file.DRIVE_FIXED : "Fixed\nDrive has fixed (nonremovable) media. This includes all hard drives, including hard drives that are removable.",
win32file.DRIVE_REMOTE : "Remote\nNetwork drives. This includes drives shared anywhere on a network.",
win32file.DRIVE_CDROM : "CDROM\nDrive is a CD-ROM. No distinction is made between read-only and read/write CD-ROM drives.",
win32file.DRIVE_RAMDISK : "RAMDisk\nDrive is a block of random access memory (RAM) on the local computer that behaves like a disk drive.",
win32file.DRIVE_NO_ROOT_DIR : "The root directory does not exist."
}
drives = win32api.GetLogicalDriveStrings().split('\x00')[:-1]
for device in drives:
type = win32file.GetDriveType(device)
print("Drive: %s" % device)
print(drive_types[type])
print("-"*72)
os.system('pause')
Your USB devices have the type win32file.DRIVE_REMOVABLE
- so this is what you're looking for. Instead of printing all drives and types, you could insert an if
condition to only process such removable devices.
Please note: SD-Cards and other removable storage media has the same drive type.
HTH!
Update, 13. July 2020:
To get further Inforrmations about connected Devices, have a look at the WMI Module for Python.
Check this example outputs, they list different informations about devices, including Manufacturer Descriptions, Serial Numbers and so on:
import wmi
c = wmi.WMI()
for item in c.Win32_PhysicalMedia():
print(item)
for drive in c.Win32_DiskDrive():
print(drive)
for disk in c.Win32_LogicalDisk():
print(disk)
os.system('pause')
To access a specific Information listed in this output, use the displayed Terms for direct access. Example:
for disk in c.Win32_LogicalDisk():
print(disk.Name)
回答2:
when I'm connect a mouse, keyboard, pen drive or printer through the usb i want this kind of name...
It's called "Friendly Name" and you can use:
import subprocess, json
out = subprocess.getoutput("PowerShell -Command \"& {Get-PnpDevice | Select-Object Status,Class,FriendlyName,InstanceId | ConvertTo-Json}\"")
j = json.loads(out)
for dev in j:
print(dev['Status'], dev['Class'], dev['FriendlyName'], dev['InstanceId'] )
Unknown HIDClass HID-compliant system controller HID\VID_046D&PID_C52B&MI_01&COL03\9&232FD3F1&0&0002
OK DiskDrive WD My Passport 0827 USB Device USBSTOR\DISK&VEN_WD&PROD_MY_PASSPORT_0827&REV_1012\575836314142354559545058&0
...
来源:https://stackoverflow.com/questions/58857920/how-to-get-connected-usb-device-list-from-windows-by-using-python-or-cmd