os.path.walk arg

Graham Fawcett graham__fawcett at hotmail.com
Sat Mar 15 01:32:53 EST 2003


Steven Taschuk <staschuk at telusplanet.net> wrote in message news:<mailman.1047624113.21268.python-list at python.org>...
> Quoth Erik Max Francis:
>   [...]

> It is fairly common, and makes sense in C; but it seems a little
> odd in Python.  If os.path.walk had no such provision, but I had a
> visitor function that needed a context object, then I'd just do
> 	walk(path, lambda d,n: visit(context, d, n))
> or the like, and wouldn't think twice about it.


Jason Orendorff has a wonderfully Pythonic replacement for os.path at 

    http://www.jorendorff.com/articles/python/path/

A short example from the page above:

    from path import path  # Jason's path, not os.path
    dir = path(os.environ['HOME'])
    for f in dir.walkfiles('*~'):
        f.remove()

Each of the path.walk... methods (one for files, one for directories,
one for both) returns a generator that yields the elements in the path
tree. The globbing pattern is optional.

I'd love to see 'path' become a standard module. Maybe it could be a
subpackage of os.path... though "from os.path.path import path" does
seem a *bit* verbose. ;-)

-- Graham




More information about the Python-list mailing list