enumerate drive names on windows

SBrunning at trisystems.co.uk SBrunning at trisystems.co.uk
Mon Nov 10 07:01:23 EST 2003


> From:	Emile van Sebille [SMTP:emile at fenx.com]
> Frank Bechmann asks:
> > is there some python standard or 'Python for Windows Extensions'
> > functionality to iterate or list-return the available drives ('A:',
> > 'B:', 'C:', ...) on a machine running windows?
> 
> win32api.GetLogicalDriveStrings()
> 
> returns a \x00 delimited string.  Use
> 
> win32api.GetLogicalDriveStrings()[:-1].split('\x00')
> 
> to create a list.
 
If you care about what *kind* of drives you get, there are a number of drive
type constants defined:

* win32file.DRIVE_CDROM
* win32file.DRIVE_FIXED
* win32file.DRIVE_NO_ROOT_DIR
* win32file.DRIVE_RAMDISK
* win32file.DRIVE_REMOTE
* win32file.DRIVE_REMOVABLE
* win32file.DRIVE_UNKNOWN

So, for example, if you wanted just the local drives, you could do:

localdrives = [drive for drive in
win32api.GetLogicalDriveStrings().split('\x00')[:-1] if
win32file.GetDriveType(drive) in [win32file.DRIVE_FIXED,
win32file.DRIVE_CDROM, win32file.DRIVE_REMOVABLE]]

Cheers,
Simon B,
http://www.brunningonline.net/simon/blog/




-----------------------------------------------------------------------
The information in this email is confidential and may be legally privileged.
It is intended solely for the addressee. Access to this email by anyone else
is unauthorised. If you are not the intended recipient, any disclosure,
copying, distribution, or any action taken or omitted to be taken in
reliance on it, is prohibited and may be unlawful. TriSystems Ltd. cannot
accept liability for statements made which are clearly the senders own.





More information about the Python-list mailing list