how to enumerate all drives on Win32?

Steve Holden sholden at holdenweb.com
Tue May 1 10:00:34 EDT 2001


"Wolfgang Strobl" <ws at mystrobl.de> wrote in message
news:r1ctetgsnk915avfu6c459s3hv3lqghep6 at 4ax.com...
> On Mon, 30 Apr 2001 19:11:09 GMT, "Steve  Holden" <sholden at holdenweb.com>
> wrote in <hNiH6.47579$qc2.12456011 at typhoon.southeast.rr.com>:
>
> ><skip at pobox.com> wrote in message
> >news:mailman.988655383.28858.python-list at python.org...
> >>
> >> For a file/directory browser I need to be able to traverse all mounted
> >> filesystems.  This is no problem on Unix systems, because we have the
studly
> >> mount(2) system call and thus have a single directory tree rooted at /.
> >>
> >> Alas, Windows (once again) is broken in this regard.  I need to know
how to
> >> enumerate all the drive letters that have something mounted on them.  I
> >> didn't see anything in the ntpath module that looked like it would do
the
> >> trick.  Any suggestions?
> >>
> >> Thanks,
> >>
> >http://www.faqts.com/knowledge-base/view.phtml/aid/4670
>
> or, sligthly extended:
>
> import win32api,string
> import win32file,win32wnet
>
> drives=win32api.GetLogicalDriveStrings()
> drives=string.splitfields(drives,'\000')
>
> drivetype={win32file.DRIVE_CDROM:"cdrom",
>        win32file.DRIVE_FIXED:"fixed",
>        win32file.DRIVE_NO_ROOT_DIR:"no_root_dir",
>        win32file.DRIVE_RAMDISK:"ramdisk",
>        win32file.DRIVE_REMOTE:"remote",
>        win32file.DRIVE_REMOVABLE:"removable",
>        win32file.DRIVE_UNKNOWN:"unknown"}
>
> for d in drives:
>     if d:
>         print d,"is",drivetype[win32file.GetDriveType(d)],
>         if win32file.GetDriveType(d)==win32file.DRIVE_REMOTE:
>             print "on",win32wnet.WNetGetUniversalName(d),
>         print
>
> That snipped produces
>
> A:\ is removable
> C:\ is fixed
> D:\ is fixed
> M:\ is cdrom
> N:\ is cdrom
> Q:\ is fixed
> U:\ is remote on \\wbox\import\
>
Interesting. I presume my Win32wnet is out of date, since on my system it
produced:

a:\ is removable
c:\ is fixed
d:\ is fixed
h:\ is cdrom
r:\ is remote on
Traceback (most recent call last):
  File "windrives.py", line 19, in ?
    print "on",win32wnet.WNetGetUniversalName(d),
AttributeError: WNetGetUniversalName

regards
 Steve





More information about the Python-list mailing list