file system iteration

Rob Williscroft rtw at freenet.co.uk
Mon Oct 9 16:22:29 CEST 2006


Tim Golden wrote in news:mailman.119.1160403292.11739.python-
list at python.org in comp.lang.python:

> [Rick]
>| Searching for a file by name. Scanning for viruses. 
> Etc. 
>| There are lots 
>| of legitimate reason to walk all paths from a centra
> l 
>| starting point, no???
> 
> Well, to get you started, I think this is the kind
> of thing you'll want. Uses ctypes, which is built-in
> to Python 2.5 so presumably legit.
> 
> <code>
> import ctypes
> import string
> 
> GetDriveType = ctypes.windll.kernel32.GetDriveTypeA
> 
> for letter in string.uppercase:
>   print letter, "=>", GetDriveType ("%s:" % letter)
> 
> </code>
> 
> You'll have to refer to 
> 
> http://windowssdk.msdn.microsoft.com/en-us/library/ms685874.aspx
> 
> and various headers to get the values in question, but they
> look quite straightforward at a glance.
> 
These seem to map when tested on my system:

import ctypes

DRIVE_TYPE_MAP = dict( enumerate ( """\
DRIVE_UNKNOWN
DRIVE_NO_ROOT_DIR
DRIVE_REMOVABLE
DRIVE_FIXED
DRIVE_REMOTE
DRIVE_CDROM
DRIVE_RAMDISK 
""".split()
))

## if you need the constants ...
for i, name in DRIVE_TYPE_MAP.iteritems():
  exec( "%s = %d" %( name, i) )

GetDriveTypeW = ctypes.windll.kernel32.GetDriveTypeW
for i in range( ord( 'A' ), 1 + ord('Z') ):
  path = u"%c:\\" % chr( i )
  print path, DRIVE_TYPE_MAP[ GetDriveTypeW( path ) ]

Rob.
-- 
http://www.victim-prime.dsl.pipex.com/



More information about the Python-list mailing list