FAQ
This MUST have been asked before, but I can't seem to Google the right
thing. ?How can I get a list of drives on a Windows box, like ["C:\",
"D:\"], like I can if I do something like listdir("/Volumes") on a Mac?

Thanks!

Bob

Search Discussions

  • Tim Golden at May 20, 2008 at 7:45 pm

    Bob Greschke wrote:
    This MUST have been asked before, but I can't seem to Google the right
    thing. How can I get a list of drives on a Windows box, like ["C:\",
    "D:\"], like I can if I do something like listdir("/Volumes") on a Mac?
    A couple of options to get the ball rolling:

    1) win32api.GetLogicalDriveStrings()

    2) http://timgolden.me.uk/python/wmi_cookbook.html#find-drive-types


    TJG
  • Mensanator at May 20, 2008 at 7:45 pm

    On May 20, 2:18?pm, Bob Greschke wrote:
    This MUST have been asked before, but I can't seem to Google the right
    thing. ?How can I get a list of drives on a Windows box, like ["C:\",
    "D:\"], like I can if I do something like listdir("/Volumes") on a Mac?

    Thanks!

    Bob
    import os.path
    dl = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    drives = ['%s:' % d for d in dl if os.path.exists('%s:' % d)]
    drives
    ['C:', 'D:', 'F:', 'G:', 'H:', 'I:', 'J:', 'M:', 'N:', 'O:', 'P:',
    'Q:', 'R:', 'S:', 'T:', 'U:', 'X:', 'Y:']
  • Python at May 20, 2008 at 8:01 pm
    Mensanator,

    <snip>
    import os.path
    dl = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    drives = ['%s:' % d for d in dl if os.path.exists('%s:' % d)]
    drives
    </snip>

    Very clever!

    Malcolm
  • Mike Driscoll at May 20, 2008 at 7:58 pm

    On May 20, 2:45?pm, Tim Golden wrote:
    Bob Greschke wrote:
    This MUST have been asked before, but I can't seem to Google the right
    thing. ?How can I get a list of drives on a Windows box, like ["C:\",
    "D:\"], like I can if I do something like listdir("/Volumes") on a Mac?
    A couple of options to get the ball rolling:

    1) win32api.GetLogicalDriveStrings()

    I gave this a go to see how it worked and ti gave me this:

    'A:\\\x00C:\\\x00D:\\\x00G:\\\x00I:\\\x00L:\\\x00P:\\\x00Q:\\\x00R:\\
    \x00U:\\\x00X:\\\x00Y:\\\x00Z:\\\x00'

    Not exactly what I expected. Do I have to parse out the "\\\x00"
    myself or is there an information level switch I should add?
    2)http://timgolden.me.uk/python/wmi_cookbook.html#find-drive-types

    This works as advertised. I was actually going to post that, but you
    beat me to it. :)
    TJG

    Mike
  • Christian Heimes at May 20, 2008 at 8:43 pm

    Mike Driscoll schrieb:
    On May 20, 2:45 pm, Tim Golden wrote:
    Bob Greschke wrote:
    This MUST have been asked before, but I can't seem to Google the right
    thing. How can I get a list of drives on a Windows box, like ["C:\",
    "D:\"], like I can if I do something like listdir("/Volumes") on a Mac?
    A couple of options to get the ball rolling:

    1) win32api.GetLogicalDriveStrings()

    I gave this a go to see how it worked and ti gave me this:

    'A:\\\x00C:\\\x00D:\\\x00G:\\\x00I:\\\x00L:\\\x00P:\\\x00Q:\\\x00R:\\
    \x00U:\\\x00X:\\\x00Y:\\\x00Z:\\\x00'

    Not exactly what I expected. Do I have to parse out the "\\\x00"
    myself or is there an information level switch I should add?
    The data is separated by NUL bytes. Split it with \x00 and you'll get a
    list of drives:
    >>
    "A:\\\x00C:\\\x00D:\\\x00G:\\\x00I:\\\x00L:\\\x00P:\\\x00Q:\\\x00R:\\\x00U:\\\x00X:\\\x00Y:\\\x00Z:\\\x00".split("\x00")
    ['A:\\', 'C:\\', 'D:\\', 'G:\\', 'I:\\', 'L:\\', 'P:\\', 'Q:\\', 'R:\\',
    'U:\\', 'X:\\', 'Y:\\', 'Z:\\', '']

    Christian
  • Mike Driscoll at May 20, 2008 at 9:16 pm

    On May 20, 3:43?pm, Christian Heimes wrote:
    Mike Driscoll schrieb:


    On May 20, 2:45 pm, Tim Golden wrote:
    Bob Greschke wrote:
    This MUST have been asked before, but I can't seem to Google the right
    thing. ?How can I get a list of drives on a Windows box, like ["C:\",
    "D:\"], like I can if I do something like listdir("/Volumes") on a Mac?
    A couple of options to get the ball rolling:
    1) win32api.GetLogicalDriveStrings()
    I gave this a go to see how it worked and ti gave me this:
    'A:\\\x00C:\\\x00D:\\\x00G:\\\x00I:\\\x00L:\\\x00P:\\\x00Q:\\\x00R:\\
    \x00U:\\\x00X:\\\x00Y:\\\x00Z:\\\x00'
    Not exactly what I expected. Do I have to parse out the "\\\x00"
    myself or is there an information level switch I should add?
    The data is separated by NUL bytes. Split it with \x00 and you'll get a
    list of drives:

    "A:\\\x00C:\\\x00D:\\\x00G:\\\x00I:\\\x00L:\\\x00P:\\\x00Q:\\\x00R:\\\x00U:\\\x00X:\\\x00Y:\\\x00Z:\\\x00".split("\x00")
    ['A:\\', 'C:\\', 'D:\\', 'G:\\', 'I:\\', 'L:\\', 'P:\\', 'Q:\\', 'R:\\',
    'U:\\', 'X:\\', 'Y:\\', 'Z:\\', '']

    Christian
    Yeah, I noticed that. I just thought that was a weird piece of info
    for the call to return.

    Mike
  • Bob Greschke at May 22, 2008 at 5:32 pm

    On 2008-05-20 13:18:08 -0600, Bob Greschke <bob at passcal.nmt.edu> said:

    This MUST have been asked before, but I can't seem to Google the right
    thing. ?How can I get a list of drives on a Windows box, like ["C:\",
    "D:\"], like I can if I do something like listdir("/Volumes") on a Mac?

    Thanks!

    Bob
    Thanks for the ideas, guys! It's off to the emacs...

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedMay 20, '08 at 7:18p
activeMay 22, '08 at 5:32p
posts8
users6
websitepython.org

People

Translate

site design / logo © 2022 Grokbase