Listing partitions (on win32)

C

Claude Henchoz

Hi

Is there any way of listing partitions on a (win32) computer without
using WMI?

Cheers, Claude
 
B

Bengt Richter

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)
... return [c+':' for c in (chr(n) for n in xrange(ord('A'), ord('Z')+1))
... if os.popen('dir %s:\\xxx'%c).read()]
... ['C:', 'D:', 'E:', 'V:', 'W:']

Regards,
Bengt Richter
 
T

Tim Golden

Claude said:
Is there any way of listing partitions on a (win32) computer without
using WMI?

Not that this answers your question, but why _don't_ you
want to use WMI?

TJG
 
B

bonono

Tim said:
Not that this answers your question, but why _don't_ you
want to use WMI?

TJG

Traceback (most recent call last):
File "<pyshell#0>", line 1, in -toplevel-
import wmi
ImportError: No module named wmi
 
T

Tim Golden

Traceback (most recent call last):
File "<pyshell#0>", line 1, in -toplevel-
import wmi
ImportError: No module named wmi

It's quite possible to do WMI in Python without using
the wmi module -- all the module does is to hide some
slightly messy plumbing. But the OP doesn't suggest
that he's unwilling to install anything, merely to *use*
WMI. (Unless you _are_ the OP under a different alias).

Just to make it plain: it's no skin off my nose at all. I
don't get offended because someone doesn't want to
use WMI. I'm merely curious as to whether it was
because the wmi module was too hard, or whether
the machines were Win9x or NT, where you have to
go out of your way to install WMI, or whether there
was some other reason.

Tim
 
B

bonono

Tim said:
It's quite possible to do WMI in Python without using
the wmi module -- all the module does is to hide some
slightly messy plumbing. But the OP doesn't suggest
that he's unwilling to install anything, merely to *use*
WMI. (Unless you _are_ the OP under a different alias).

Just to make it plain: it's no skin off my nose at all. I
don't get offended because someone doesn't want to
use WMI. I'm merely curious as to whether it was
because the wmi module was too hard, or whether
the machines were Win9x or NT, where you have to
go out of your way to install WMI, or whether there
was some other reason.
I am not him, but just another data point(possibly reason) of why one
doesn't want to use it. It is not intended to be a post of "wmi suck".
In fact, when I first saw the post about wmi module, I immediate tried
to query my XP using it but once I saw the above, I gave up.
 
T

Tim Golden

I am not him, but just another data point(possibly reason) of why one
doesn't want to use it. It is not intended to be a post of "wmi suck".
In fact, when I first saw the post about wmi module, I immediate tried
to query my XP using it but once I saw the above, I gave up.

Fair enough. You'd need to install the pywin32 extensions
and then the wmi module on top of that. But I suppose that
because I simply install pywin32 on any Windows box as soon
as I install Python, I assumed others did the same. Not
necessarily the case, obviously. Also, I'm obviously
guaranteed to have the wmi module available since I'm
its author, but again not everyone has.

Just in case you're interested in trying it out:
http://timgolden.me.uk/python/wmi.html

I think the thing with WMI -- the mechanism, not the module --
is that you can do lots of things easily, or at least
consistently, which are painful otherwise. Sometimes
people will ask, eg "How do I copy a file with WMI?"
and I'm inclined to say: unless you have some very
specific requirement, you probably don't want to use
WMI to do that. But for certain things, it's just so
easy -- and there are examples and recipes for it all
over the web -- that it just makes sense.

Tim
 
T

Tim Golden

Claude said:
Hi

Is there any way of listing partitions on a (win32) computer without
using WMI?

It looks as though XP has a command-line utility
called diskpart.exe which should be able to do
this kind of thing. I've no experience with it
myself, but assuming that it outputs information
to screen, it should be easy to pull that back
into Python with os.popen or the subprocess module.

TJG
 
R

Roger Upole

Bengt Richter said:
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)
... return [c+':' for c in (chr(n) for n in xrange(ord('A'), ord('Z')+1))
... if os.popen('dir %s:\\xxx'%c).read()]
...['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.

Roger
 
C

Claude Henchoz

Why I don't want to rely on WMI:
I am wanting to use this under Windows PE, and although I can include
WMI, I just wanted to find out whether there is a way to not use it.

Another thing:
I really do want to also get the partitions that do _not_ have an
associated drive letter.

All in all:
I am probably going to have to write something with tempfile that uses
diskpart.exe to use this. Although this will get ugly, it seems to be
the best way (the best way without WMI, that is).

Thanks for the help guys!

Cheers, Claude
 
B

Bengt Richter

Bengt Richter said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top