tim's idea makes my code crawl: (was: Compiling Python 1.6 under MacOS X ...)

Grant Griffin g2 at seebelow.org
Mon Sep 25 16:40:55 EDT 2000


Tim Peters wrote:
> 
...
> 2. I almost always use this kind of scheme for crawling over a directory
> tree, rather than a recursive scheme or os.path.walk.  Manipulating an
> explicit list of directories yet to be visited is very flexible, easy to
> write, and allows (as in the example) major optimizations specific to the
> task at hand.  Maybe it's just me, but I can also recreate this scheme from
> scratch much faster than I can ever remember how to *use* os.path.walk!
> Many "frameworks" in Python suffer a similar fate -- it's so easy to do it
> "by hand" they never get used.
...

> def crawl(root):
>     from os.path import join, exists, isdir
>     from os import listdir, chdir
>     dirs = [root]
>     while dirs:
>         dir = dirs.pop()
>         # This seems a little-known trick in Python:  changing to the
>         # current directory allows the subsequent isdir-in-a-loop to
>         # work on just the file name, instead of making the OS reparse
>         # the whole darn path from the root again each time.
>         # Depending on the OS, can save gobs of time.
>         chdir(dir)
>         for f in filter(isdir, listdir(dir)):
>             path = join(dir, f)
>             if f == "CVS":
>                 entries = join(path, "Entries")
>                 if exists(entries):
>                     process(entries)
>                 else:
>                     raise TypeError("CVS dir w/o Entries!! " + path)
>             else:
>                 dirs.append(path)

Hi Tim,

I thought that was a pretty neat idea.  However, when I tried it out
(with some modifications for my application), I found that it didn't
work right in the case where the directory I gave it was ".".  Is that
just a cockpit problem on my part, or is that actually a "feature" of
the above?

If the latter, how should we fix it?  I thought about trapping "." out
as a special case, then substituting the full path, but is there
something better?

beautiful-would-be-better-than-ug-ly y'rs,

=g2
-- 
_____________________________________________________________________

Grant R. Griffin                                       g2 at dspguru.com
Publisher of dspGuru                           http://www.dspguru.com
Iowegian International Corporation	      http://www.iowegian.com



More information about the Python-list mailing list