Python:获取 USB 闪存驱动器设备的名称 [windows] [英] Python: get name of a USB flash drive device [windows]

查看:55
本文介绍了Python:获取 USB 闪存驱动器设备的名称 [windows]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写能够读取有关 REMOVEABLE_DEVICE (USB) 的一些信息的小程序.我试过 pyusb 但我无法提取我需要的数据.

我想从系统中读取 USB 设备的名称.

采用这种格式:

USB Flash Memory - 这是型号信息可移动磁盘 (H:) - 这是设备的名称通用闪存盘USB 磁盘 (F:)雷克沙 USB 闪存盘雷克沙 (I:)

我能够使用 win32com.client 库获取模型信息,其灵感来自 此处,但我无法获取 Windows 资源管理器中显示的设备名称.

也许我使用了错误的库?

这是我的代码:

导入 win32com.clientstrComputer = "."objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")objSWbemServices = objWMIService.ConnectServer(strComputer,"root\cimv2")colItems = objSWbemServices.ExecQuery("SELECT * FROM Win32_DiskDrive WHERE InterfaceType = \"USB\"")对于 colItems 中的 objItem:如果 objItem.Caption != 无:打印标题:"+`objItem.Caption[:-11]`

这是 Windows Win32_DiskDrive 类的链接:链接

预先感谢您的帮助.

解决方案

免责声明:我对 win32com.client 库没有任何经验.

像您一样从 Win32_DiskDrive 开始,我浏览了 Win32_DiskDriveToDiskPartitionWin32_LogicalDiskToPartition,然后到 Win32_LogicalDisk 获取VolumeName这似乎是你想要的.

导入 win32com.clientstrComputer = "."objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")objSWbemServices = objWMIService.ConnectServer(strComputer,"root\cimv2")# 1. Win32_DiskDrivecolItems = objSWbemServices.ExecQuery("SELECT * FROM Win32_DiskDrive WHERE InterfaceType = \"USB\"")DiskDrive_DeviceID = colItems[0].DeviceID.replace('\\', '').replace('.', '')DiskDrive_Caption = colItems[0].Caption打印 'DiskDrive DeviceID:', DiskDrive_DeviceID# 2. Win32_DiskDriveToDiskPartitioncolItems = objSWbemServices.ExecQuery("SELECT * from Win32_DiskDriveToDiskPartition")对于 colItems 中的 objItem:如果 DiskDrive_DeviceID 在 str(objItem.Antecedent):DiskPartition_DeviceID = objItem.Dependent.split('=')[1].replace('"', '')打印 'DiskPartition DeviceID:', DiskPartition_DeviceID# 3. Win32_LogicalDiskToPartitioncolItems = objSWbemServices.ExecQuery("SELECT * from Win32_LogicalDiskToPartition")对于 colItems 中的 objItem:如果 DiskPartition_DeviceID 在 str(objItem.Antecedent):LogicalDisk_DeviceID = objItem.Dependent.split('=')[1].replace('"', '')打印 'LogicalDisk DeviceID:', LogicalDisk_DeviceID# 4. Win32_LogicalDiskcolItems = objSWbemServices.ExecQuery("SELECT * from Win32_LogicalDisk WHERE DeviceID=\"" + LogicalDisk_DeviceID + "\"")打印 'LogicalDisk VolumeName:', colItems[0].VolumeName打印# 把它放在一起打印 DiskDrive_Caption打印 colItems[0].VolumeName, '(' + LogicalDisk_DeviceID + ')'

对我有用:

DiskDrive DeviceID: PHYSICALDRIVE1磁盘分区设备 ID:磁盘 #1,分区 #0逻辑磁盘设备 ID:D:逻辑磁盘卷名称:PENDRIVE索尼存储媒体 USB 设备潘德瑞夫 (D:)

这似乎提供了一种复杂但可行的方法,也许您可​​以进一步简化它.我唯一的简化是省略了 Win32_DiskPartition 因为我们只需要连接.

请注意:

  • 我不确定解压 \\.\PHYSICALDRIVE1 之类的东西的干净"方式是什么,但应该可以摆脱 .replace()代码>-方法.
  • 我不确定是否可以将步骤 2 和 3 中的循环集成到查询中?这也会大大简化它(也许可以像 SQL 一样JOIN它们?).
  • (以上代码仅适用于单个 USB 驱动器.)

I'm trying to write little program which will be able to read some informations about REMOVEABLE_DEVICE (USB). I've tried pyusb but I was not able to pull data I need.

I would like to read from the system the name of the USB device.

In this format:

USB Flash Memory - this is the model information
Removable Disk (H:) - this is the name of device

Generic Flash Disk
USB DISK (F:)

Lexar USB Flash Drive
Lexar (I:)

I am able to get the model information with win32com.client library, inspired from here, but I am not able to get name of the device shown in Windows explorer.

Maybe I am using wrong library?

Here is my code:

import win32com.client
strComputer = "."
objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")
objSWbemServices = objWMIService.ConnectServer(strComputer,"root\cimv2")
colItems = objSWbemServices.ExecQuery("SELECT * FROM Win32_DiskDrive WHERE InterfaceType = \"USB\"")
for objItem in colItems:
    if objItem.Caption != None:
        print "Caption:" + ` objItem.Caption[:-11]`

Here is link for Windows Win32_DiskDrive Class: link

Thank you in advance for your help.

解决方案

Disclaimer: I haven't really got any experience with the win32com.client library.

By starting with Win32_DiskDrive like you did, I went over Win32_DiskDriveToDiskPartition and Win32_LogicalDiskToPartition, and then to Win32_LogicalDisk to get the VolumeName which seems what you want.

import win32com.client
strComputer = "."
objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")
objSWbemServices = objWMIService.ConnectServer(strComputer,"root\cimv2")

# 1. Win32_DiskDrive
colItems = objSWbemServices.ExecQuery("SELECT * FROM Win32_DiskDrive WHERE InterfaceType = \"USB\"")
DiskDrive_DeviceID = colItems[0].DeviceID.replace('\\', '').replace('.', '')
DiskDrive_Caption = colItems[0].Caption

print 'DiskDrive DeviceID:', DiskDrive_DeviceID

# 2. Win32_DiskDriveToDiskPartition
colItems = objSWbemServices.ExecQuery("SELECT * from Win32_DiskDriveToDiskPartition")
for objItem in colItems:
    if DiskDrive_DeviceID in str(objItem.Antecedent):
        DiskPartition_DeviceID = objItem.Dependent.split('=')[1].replace('"', '')

print 'DiskPartition DeviceID:', DiskPartition_DeviceID

# 3. Win32_LogicalDiskToPartition
colItems = objSWbemServices.ExecQuery("SELECT * from Win32_LogicalDiskToPartition")
for objItem in colItems:
    if DiskPartition_DeviceID in str(objItem.Antecedent):
        LogicalDisk_DeviceID = objItem.Dependent.split('=')[1].replace('"', '')

print 'LogicalDisk DeviceID:', LogicalDisk_DeviceID

# 4. Win32_LogicalDisk
colItems = objSWbemServices.ExecQuery("SELECT * from Win32_LogicalDisk WHERE DeviceID=\"" + LogicalDisk_DeviceID + "\"")
print 'LogicalDisk VolumeName:', colItems[0].VolumeName
print

# putting it together
print DiskDrive_Caption
print colItems[0].VolumeName, '(' + LogicalDisk_DeviceID + ')'

Works for me:

DiskDrive DeviceID: PHYSICALDRIVE1
DiskPartition DeviceID: Disk #1, Partition #0
LogicalDisk DeviceID: D:
LogicalDisk VolumeName: PENDRIVE

Sony Storage Media USB Device
PENDRIVE (D:)

This seems to provide a complicated, but possible way, maybe you can simplify it even more. My only simplification is leaving out Win32_DiskPartition already because we only need the connection.

Please note:

  • I'm not sure what's the "clean" way to unpack something like \\.\PHYSICALDRIVE1, but it should be possible to get rid of the .replace()-methods.
  • I'm not sure if it's possible to integrate the loops in steps 2 and 3 into the query? That would also simplify it a lot (maybe it's possible to JOIN them SQL-like?).
  • (The code above will only work for a single USB drive.)

这篇关于Python:获取 USB 闪存驱动器设备的名称 [windows]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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