SNA*_*lim 6 python windows usb cmd device
我需要使用 python 或 cmd 从 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()
Run Code Online (Sandbox Code Playgroud)
结果我得到这个:
['USB Root Hub (USB 3.0)', 'USB Composite Device', 'USB Composite Device']
Run Code Online (Sandbox Code Playgroud)
但我没有得到一个有意义的全名。
对于 cmd 我也在尝试这个:
wmic path CIM_LogicalDevice where "Description like 'USB%'" get /value
Run Code Online (Sandbox Code Playgroud)
再次,我没有得到连接的 USB 设备的任何含义的全名。
当我通过 USB 连接鼠标、键盘、笔式驱动器或打印机时,我想要这种名称。就像“a4tech mouse”,或者即使我只得到“mouse”也没关系。这种类型的名称出现在 Windows 10 设置的设备部分中。但我得到“USB Root Hub (USB 3.0)”、“USB Composite Device”,这实际上没有任何意义。用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')
Run Code Online (Sandbox Code Playgroud)
您的 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')
Run Code Online (Sandbox Code Playgroud)
要访问此输出中列出的特定信息,请使用显示的条款进行直接访问。例子:
for disk in c.Win32_LogicalDisk():
print(disk.Name)
Run Code Online (Sandbox Code Playgroud)
当我通过 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'] )
Run Code Online (Sandbox Code Playgroud)
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
...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
30409 次 |
| 最近记录: |