繁体   English   中英

如何使用 python 或 ZDFFF0A7FA1A55C8C1A4966C19F6DA425 从 windows 连接 USB 设备列表

[英]How to get connected USB device list from windows by using python or cmd

我需要使用 python 或 ZDFFF0A7FA1A55C8C1A2ZZ966C19F6DA5 从 windows 连接 USB 设备列表。

对于 python 我正在尝试这个。

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

结果我得到了这个:

['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 鼠标”或者即使我得到“鼠标”也可以。 这种类型的名称出现在 windows 10 设置的设备部分。但我得到“USB Root Hub (USB 3.0)”、“USB 复合设备”,实际上没有任何意义。 python 可以吗?

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

不确定它是否是您要查找的内容,但是在 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 卡和其他可移动存储介质具有相同的驱动器类型。


2020 年 7 月 13 日更新:

要获取有关连接设备的更多信息,请查看 Python 的 WMI 模块

检查此示例输出,它们列出了有关设备的不同信息,包括制造商描述、序列号等:

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

要访问此 output 中列出的特定信息,请使用显示的条款进行直接访问。 例子:

for disk in c.Win32_LogicalDisk():
    print(disk.Name)

当我通过 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'] )

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
...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM