如何通过使用python或cmd从windows获得连接的USB设备列表

5 人关注

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():
        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()

结果,我得到了这个结果。

['USB Root Hub (USB 3.0)', 'USB Composite Device', 'USB Composite Device']

但我没有得到一个含义完整的名字。

对于cmd,我也在尝试这样做。

wmic path CIM_LogicalDevice where "Description like 'USB%'" get /value

而且我也没有得到任何关于连接的USB设备的全名。

当我通过USB连接鼠标、键盘、笔式驱动器或打印机时,我希望有这样的名字,比如 "A4tech鼠标",或者即使我只得到 "鼠标",也是可以的。但我得到的是 "USB Root Hub (USB 3.0)"、"USB Composite Device",这实际上没有任何意义。 有可能用python吗?

如果有人知道这个答案,请帮助。这对我来说非常重要。

2 个评论
"但我没有得到一个含义完整的名字。" - 你在期待什么?
python
windows
cmd
usb
device
SNA Nilim
SNA Nilim
发布于 2019-11-14
2 个回答
xph
xph
发布于 2019-11-14
已采纳
0 人赞同

不知道这是否是你要找的东西,但在Windows 10上使用Python 3与 pywin32 ,你可以用它来获得所有的驱动器字母和类型。

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')

你的USB设备的类型为win32file.DRIVE_REMOVABLE。- 所以这就是你要找的东西。与其打印所有的驱动器和类型,你可以插入一个if的条件,只处理这样的可移动设备。

请注意。SD卡和其他可移动存储介质具有相同的驱动器类型。

更新,13。2020年7月。

要获得有关连接设备的进一步信息,请看WMI Module for Python.

检查这个例子的输出,它们列出了关于设备的不同信息,包括制造商描述、序列号等等。

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')

要访问该输出中列出的特定信息,请使用所显示的术语进行直接访问。例子。

for disk in c.Win32_LogicalDisk():
    print(disk.Name)
    
你能告诉我们如何获得可移动设备的设备ID和产品吗?
xph
@S.I.J: 我已经更新了答案,有一个模块可以帮助你获得你需要的信息。
Pedro Lobito
Pedro Lobito
发布于 2019-11-14
0 人赞同

当我通过usb连接鼠标、键盘、笔式驱动器或打印机时,我希望有这样的名字...

它被称为 "友好名称",你可以使用。

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'] )