A little assistance with os.walk please.

Tim Chase python.list at tim.thechases.com
Mon Aug 14 13:49:27 EDT 2006


>  >>> allowed = ['.txt', '.sql']
>  >>> for path, dirs, files in os.walk("."):
> ...     for f in files:
> ...             if splitext(f)[1].lower() not in allowed: continue

Additionally, if you really do want to specify wildcards:

 >>> allowed = ['*.txt', 'README*', '*.xml', '*.htm*']
 >>> from glob import fnmatch
 >>> import os
 >>> for path, dirs, files in os.walk("."):
...     good_files = []
...     for pat in allowed:
...             good_files.extend(fnmatch.filter(files, pat))
...             if good_files: break
...     for f in good_files:
...             print "doing something with %s" % 
os.path.join(path, f)

-tkc










More information about the Python-list mailing list