像您一样从 Win32_DiskDrive 开始,我浏览了 Win32_DiskDriveToDiskPartition 和 Win32_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 informationRemovable Disk (H:) - this is the name of deviceGeneric Flash DiskUSB DISK (F:)Lexar USB Flash DriveLexar (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.clientstrComputer = "."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: linkThank 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.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].Captionprint 'DiskDrive DeviceID:', DiskDrive_DeviceID# 2. Win32_DiskDriveToDiskPartitioncolItems = 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_LogicalDiskToPartitioncolItems = 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_LogicalDiskcolItems = objSWbemServices.ExecQuery("SELECT * from Win32_LogicalDisk WHERE DeviceID=\"" + LogicalDisk_DeviceID + "\"")print 'LogicalDisk VolumeName:', colItems[0].VolumeNameprint# putting it togetherprint DiskDrive_Captionprint colItems[0].VolumeName, '(' + LogicalDisk_DeviceID + ')'Works for me:DiskDrive DeviceID: PHYSICALDRIVE1DiskPartition DeviceID: Disk #1, Partition #0LogicalDisk DeviceID: D:LogicalDisk VolumeName: PENDRIVESony Storage Media USB DevicePENDRIVE (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]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-14 10:44