[Tutor] Excluding branches while walking directory tree
Dave Kuhlman
dkuhlman at rexx.com
Wed Sep 13 17:53:51 CEST 2006
On Wed, Sep 13, 2006 at 11:15:44AM -0400, William O'Higgins Witteman wrote:
> Hello all,
>
> I am looking for an approach for the following problem:
>
> I have to walk a directory tree and examine files within it. I have a
> set of directory names and filename patterns that I must skip while
> doing this walk. How do I create a set of rules to skip files or
> directory branches? I'm looking for something reasonably scalable,
> 'cause I'm sure to need to update these rules in the future.
>
For the first part of your problem, take a look at os.walk():
http://docs.python.org/lib/os-file-dir.html
And also glob.glob():
http://docs.python.org/lib/module-glob.html
The "in" operator is likely to be helpful in determining which
files to skip. Suppose that you have a list of file names to be
ignored:
skips = ['ignore1.txt', 'ignore2.txt']
then check a filename against the names to be skipped:
if filename not in skips:
# process the files not to be ignored here.
And, for more complex cases, you may want to write a test function
that returns True or False depending on whether the file is to be
ignored. A trivial example:
def good_file(filename, skips):
if filename in skips:
return False
else:
return True
skips = ['ignore1.txt', 'ignore2.txt']
if good_file(filename, skips):
# process good files here
o
o
o
Dave
--
Dave Kuhlman
http://www.rexx.com/~dkuhlman
More information about the Tutor
mailing list