how on earth do i get a drive list?

G. Willoughby thecalm at NOSPAM.btinternet.com
Fri Jul 27 13:55:53 EDT 2001


Thanks m8, very helpful, i didn't realise it was so platform specific! well
you live an' learn  :)

ta,

G. willoughby

"David Bolen" <db3l at fitlinxx.com> wrote in message
news:uwv4vut4n.fsf at ctwd0143.fitlinxx.com...
> "G. Willoughby" <thecalm at NOSPAM.btinternet.com> writes:
>
> > how on earth do i get a drive list? i've been searching for a command to
> > display a tuple or list containing all available drives, but i can't
find
> > it! is it just me or are the standard python docs quite confusing. oh
well
> > any ideas out there?
>
> Assuming you mean under Windows...
>
> Since the concept of "drives" is an operating-system specific issue,
> it's not something in the standard Python libraries, but something
> that you generally need to handle in an OS-specific manner.  Sometimes
> you'll find such stuff in the os module, but in this case you're
> better off with the win32all package which provides interfaces to
> windows-specific functions.
>
> win32all is a package that can be installed on top of a stock Python
> installation, or in some distributions, like ActiveState, may be
> included already.
>
> The simplest method is via the win32api module:
>
>     >>> import win32api
>     >>> import string
>     >>> string.split(win32api.GetLogicalDriveStrings(),'\0')[:-1]
>     ['A:\\', 'C:\\', 'D:\\', 'E:\\']
>
> The actual code is mildly complex, as I'm doing three things at once.
> GetLogicalDriveStrings (which is a native Windows function) returns a
> single string with the drives separated by NUL characters (\x00).  It
> also terminates the list with an extra NUL.
>
> So, I take that string, and then split it on the NULs.  This yields a
> list of which the final entry will always be the empty string ('') due
> to the extra NUL terminator, so I take a slice of that list to exclude
> the final element.
>
> There is also a GetLogicalDrives() that returns a bitmask, which may or
> may not be more suitable for your purposes.
>
> Another poster suggested iterating through possible drives using
> os.path.exist() which will also work, but will end up activating all
> the devices it tries (e.g., your floppy disk may try to be accessed,
> and/or the CDROM may spin up briefly), and since the notion of
> existence of a drive is platform specific, it's probably worth too
> much trying to keep the check platform independent.
>
> --
> -- David
> --
> /-----------------------------------------------------------------------\
>  \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
>   |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
>  /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
> \-----------------------------------------------------------------------/





More information about the Python-list mailing list