Listing partitions (on win32)

Bengt Richter bokr at oz.net
Sun Jan 15 19:01:36 EST 2006


On Sun, 15 Jan 2006 08:08:16 -0500, "Roger Upole" <rupole at hotmail.com> wrote:

>
>"Bengt Richter" <bokr at oz.net> wrote in message news:43ca001d.1137679376 at news.oz.net...
>> On 14 Jan 2006 16:52:33 -0800, "Claude Henchoz" <claude.henchoz at gmail.com> wrote:
>>
>>>Hi
>>>
>>>Is there any way of listing partitions on a (win32) computer without
>>>using WMI?
>>>
>> Maybe this will work (I skipped A: and B:, but you can include them if
>> you want to catch floppy drives with something in them). The 'xxx' is just so as
>> not to get a full directory's worth of useless text. If xxx happens to be defined
>> it doesn't hurt anything. It's not going to be as fast as using one of the
>> win32 api packages to get at GetLogicalDriveStrings, but this works on my NT4:
>> (error messages apparently go to stderr, so stdout gets '' which makes the if fail)
>>
>> >>> def fakeGetLogicalDriveStrings():
>> ...     return [c+':' for c in (chr(n) for n in xrange(ord('A'), ord('Z')+1))
>> ...                   if os.popen('dir %s:\\xxx'%c).read()]
>> ...
>> >>> fakeGetLogicalDriveStrings()
>> ['C:', 'D:', 'E:', 'V:', 'W:']
>>
>> Regards,
>> Bengt Richter
>
>This will miss any partitions that don't have a drive letter assigned.
>It will also give duplicate results for any volumes that have more
>than one drive letter.  And it will return an entry for CD or DVD
>drives which aren't disk partitions.
>
Yes, I guess I wasn't paying attention to the focus on partitions.
I wonder if you could try opening the physical disks (there is a weird
path prefix syntax IIRC) and read and interpret MBRs etc. for yourself.
Of course, you would need privilege to open raw disk reading, and you
definitely wouldn't want to make a mistake about read vs write ;-)

If ctypes becomes standard amongst the batteries included, this kind of
stuff should get easier, since I suppose you could use it to get to the
APIs you suggest (DeviceIoControl with IOCTL_DISK_GET_DRIVE_LAYOUT).

Kind of scary the damage you could do as administrator though, given that
on windows people tend to wind up running as administrator by default if they own
the machine ;-/

Regards,
Bengt Richter



More information about the Python-list mailing list