如何使用python或cmd从Windows获取已连接的USB设备列表 [英] How to get connected USB device list from windows by using python or cmd

查看:291
本文介绍了如何使用python或cmd从Windows获取已连接的USB设备列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用python或cmd从Windows获取已连接的USB设备列表.

I need to get connected USB device list from windows by using python or cmd.

对于python,我正在尝试这样做.

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

结果我得到了这个:

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

但是我没有一个全名.

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

and for cmd i'm trying this also:

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

同样,对于连接的USB设备,我也没有任何全名.

and again i don't get any meaning full name for connected usb devices.

当我通过USB连接鼠标,键盘,笔式驱动器或打印机时,我想要这种名称.像"a4tech鼠标",或者即使我得到鼠标"也没关系.这种类型的名称出现在Windows 10设置的设备部分中,但是我得到的是"USB Root Hub(USB 3.0)","USB Composite Device",实际上没有任何含义.python可能吗?

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.

推荐答案

不确定是不是您要的内容,但是在Windows 10上将Python 3与

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

您的USB设备的类型为 win32file.DRIVE_REMOVABLE -因此,这就是您想要的.除了打印所有驱动器和类型以外,您可以插入 if 条件以仅处理此类可移动设备.

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.

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

Please note: SD-Cards and other removable storage media has the same drive type.

HTH!

更新,2020年7月13日:

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

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)

这篇关于如何使用python或cmd从Windows获取已连接的USB设备列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆