[python-win32] Finding the physical USBSTOR disk size

Chris Jesse chris.jesse at flightdataservices.com
Fri Mar 19 11:30:42 CET 2010


Hello, 

I am trying to use WMI in order to get information about drives so that we can read and write bytes to raw Compact Flash cards, among other devices. The problem that we are having is that we can't seem to get an accurate size for the disks. We need to be able to get a full dump of the cards and write 0x00 to the cards in order to zero them completely but cannot do so without having an accurate size, in bytes, for the devices. 

I know that in the past we were able to do so, using the Win32_Diskpartition Size property but have recently found that this is no longer the case as it is inaccurate. We believe this is because we may not have tested it with a FAT formatted media before. 

These are the results of using a 512MB CF card with Fat16 formatting: 
-------- Correct sizes reported by Linux and Winhex -------- 
>>> 512483328 # HAL, FDISK and WinHex on Ubuntu Linux both report this size (real size) 
512483328 
>>> 512483328 # WinHex Total capacity of Physical media (real size) 
512483328 
-------- Wrong sizes reported -------- 
>>> int(partition.Size) # Win32_DiskDriveToDiskPartition - works when there are NO partitions (i.e. already initialised with \x00 throughout) 
509935104 
>>> int(drive.Size) # Win32_DiskDrive - always wrong, it's a "best guess" given info the disk provides 
509967360 
>>> 509665280 # Windows Right Click > Properties on drive 
509665280 
>>> 509665280 # WinHex Free Space of Logical media (f:) -- same as Properties (as it has fat formatting) 
509665280 
>>> 509934592 # WinHex Total capacity of Logical media (f:) 
509934592 

Do you have any ideas on how we might be able to get this size information? 

Below is some of the code that we have been using to try and get all of the disk information that we require. 

Thanks in advance, Chris. 
--- 
for drive in c.Win32_DiskDrive(): 
# Add device and its size to medias found dictionary 
# Use the MediaLoaded to add to the dictionary that it's loaded 
pnp_dev_id = drive.PNPDeviceID.split('\\') 

if pnp_dev_id[0] == 'USBSTOR': 
usb_dev = flightdataretrieval.device.GenericDevice() 
for partition in drive.associators("Win32_DiskDriveToDiskPartition"): 
for logical_disk in partition.associators("Win32_LogicalDiskToPartition"): 
usb_vol_dict = {} 
if str(logical_disk.FileSystem) != "None": 
usb_vol_dict[str(logical_disk.Caption)] = str(logical_disk.FileSystem) 
logging.debug("Drive %s size %s",str(logical_disk.FileSystem),str(drive.Size)) 
if drive.Size != None: 
# Determine the path to use for removable disk access 
logical_address = '\\\\.\\' + logical_disk.Caption 
usb_dev.location = logical_address # r'\\.\f:' 
usb_dev.physical_location = drive.DeviceID # r'\\.\PHYSICALDRIVE1' 
usb_dev.dev_type = 'USB' 
usb_dev.media_loaded = drive.MediaLoaded 
# fix 02.12.09 - use physical disk drive size, not that of the partition. 
# counter fix 18.02.10 - no, use the partition! it has the larger size reported. 
usb_dev.size = int(partition.Size) # e.g. 521773056 (512MB) 
##usb_dev.size = int(drive.Size) # e.g. 518192640 (512MB) 
usb_dev.volumes = usb_vol_dict 
usb_dev_list.append(usb_dev) 
else: pass 

-- 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-win32/attachments/20100319/e88d11db/attachment.html>


More information about the python-win32 mailing list