By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,197 Members | 1,210 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,197 IT Pros & Developers. It's quick & easy.

Listing partitions (on win32)

P: n/a
Hi

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

Cheers, Claude

Jan 15 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a
On 14 Jan 2006 16:52:33 -0800, "Claude Henchoz" <cl************@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
Jan 15 '06 #2

P: n/a
Claude Henchoz wrote:
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

Jan 15 '06 #3

P: n/a

Tim Golden wrote:
Claude Henchoz wrote:
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

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


Jan 15 '06 #4

P: n/a
bo****@gmail.com wrote:
Tim Golden wrote:
Claude Henchoz wrote:
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

import wmi
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

Jan 15 '06 #5

P: n/a

Tim Golden wrote:
bo****@gmail.com wrote:
Tim Golden wrote:
Claude Henchoz wrote:

> 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

>> import wmi


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.

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.

Jan 15 '06 #6

P: n/a
bo****@gmail.com wrote:
Tim Golden wrote:
bo****@gmail.com wrote:
Tim Golden wrote:
Claude Henchoz wrote:

> 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
>> import wmi
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).

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
Jan 15 '06 #7

P: n/a
Claude Henchoz wrote:
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
Jan 15 '06 #8

P: n/a

"Claude Henchoz" <cl************@gmail.com> wrote in message news:11*********************@z14g2000cwz.googlegro ups.com...
Hi

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

Cheers, Claude


Using win32file.DeviceIOControl with IOCTL_DISK_GET_DRIVE_LAYOUT
as the control code should be able to retrieve the partitions
defined on a disk.
http://msdn.microsoft.com/library/de...ive_layout.asp
Unpacking the output buffer may get a little messy, though.

hth
Roger

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jan 15 '06 #9

P: n/a

"Bengt Richter" <bo**@oz.net> wrote in message news:43*****************@news.oz.net...
On 14 Jan 2006 16:52:33 -0800, "Claude Henchoz" <cl************@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.

Roger

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jan 15 '06 #10

P: n/a
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

Jan 15 '06 #11

P: n/a
On Sun, 15 Jan 2006 08:08:16 -0500, "Roger Upole" <ru****@hotmail.com> wrote:

"Bengt Richter" <bo**@oz.net> wrote in message news:43*****************@news.oz.net...
On 14 Jan 2006 16:52:33 -0800, "Claude Henchoz" <cl************@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
Jan 16 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.