Listing partitions (on win32)

Discussion in 'Python' started by Claude Henchoz, Jan 15, 2006.

  1. Hi

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

    Cheers, Claude
    Claude Henchoz, Jan 15, 2006
    #1
    1. Advertising

  2. On 14 Jan 2006 16:52:33 -0800, "Claude Henchoz" <> 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
    Bengt Richter, Jan 15, 2006
    #2
    1. Advertising

  3. Claude Henchoz

    Tim Golden Guest

    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
    Tim Golden, Jan 15, 2006
    #3
  4. Claude Henchoz

    Guest

    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, 2006
    #4
  5. Claude Henchoz

    Tim Golden Guest

    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
    Tim Golden, Jan 15, 2006
    #5
  6. Claude Henchoz

    Guest

    Tim Golden wrote:
    > 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, 2006
    #6
  7. Claude Henchoz

    Tim Golden Guest

    Why/Why not WMI [was: Listing partitions (on win32)]

    wrote:
    > Tim Golden wrote:
    >> 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
    Tim Golden, Jan 15, 2006
    #7
  8. Claude Henchoz

    Tim Golden Guest

    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
    Tim Golden, Jan 15, 2006
    #8
  9. Claude Henchoz

    Roger Upole Guest

    "Claude Henchoz" <> wrote in message news:...
    > 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/d...-us/fileio/fs/ioctl_disk_get_drive_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 =----
    Roger Upole, Jan 15, 2006
    #9
  10. Claude Henchoz

    Roger Upole Guest

    "Bengt Richter" <> wrote in message news:...
    > On 14 Jan 2006 16:52:33 -0800, "Claude Henchoz" <> 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 =----
    Roger Upole, Jan 15, 2006
    #10
  11. 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
    Claude Henchoz, Jan 15, 2006
    #11
  12. On Sun, 15 Jan 2006 08:08:16 -0500, "Roger Upole" <> wrote:

    >
    >"Bengt Richter" <> wrote in message news:...
    >> On 14 Jan 2006 16:52:33 -0800, "Claude Henchoz" <> 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
    Bengt Richter, Jan 16, 2006
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    7
    Views:
    1,186
    Roedy Green
    Sep 5, 2005
  2. Replies:
    11
    Views:
    6,400
    Luc The Perverse
    Nov 19, 2005
  3. Frank Bechmann
    Replies:
    3
    Views:
    324
    Frank Bechmann
    Nov 9, 2003
  4. Steve
    Replies:
    7
    Views:
    337
    Anton Vredegoor
    Apr 30, 2004
  5. Nick J Chackowsky

    Partitions of an integer

    Nick J Chackowsky, Jul 24, 2004, in forum: Python
    Replies:
    5
    Views:
    538
    Damien Wyart
    Jul 26, 2004
Loading...

Share This Page