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.
I used the approach of @adrianus and improved a bit on it to return multiple usb drives. For how it works check his answer. For quick and dirty code that hopefully works for you check below :)
I will look odd but it will work (get list of all removable volume's
Label's
)output
Disclaimer: I haven't really got any experience with the
win32com.client
library.By starting with
Win32_DiskDrive
like you did, I went overWin32_DiskDriveToDiskPartition
andWin32_LogicalDiskToPartition
, and then toWin32_LogicalDisk
to get theVolumeName
which seems what you want.Works for me:
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:
\\.\PHYSICALDRIVE1
, but it should be possible to get rid of the.replace()
-methods.JOIN
them SQL-like?).