pythonic tree-walking idioms

Fredrik Lundh fredrik at
Thu May 17 03:50:09 EDT 2001

raskol at wrote:
> I've scoured the source of the standard 2.1 library and discovered
> os.path.walk()

os.path.walk is pretty unpythonic, imo [1].  using an iterator-style
"walker" object is usually both faster and more convenient.

an example:

From: Fredrik Lundh (fredrik at
Subject: Re: Find file functionality
Newsgroups: comp.lang.python
Date: 2000/11/19


a third alternative is to forget about walk, and use a directory
walker object instead.  here's an example from the eff-bot guide
to the standard library:

# from (the eff-bot guide to) The Python Standard Library

import os

class DirectoryWalker:

    def __init__(self, directory):
        self.stack = [directory]
        self.files = []
        self.index = 0

    def __getitem__(self, index):
        while 1:
                file = self.files[self.index]
                self.index = self.index + 1
            except IndexError:
                # pop next directory from stack.  this raises
                # an IndexError if there's nothing more to do
       = self.stack.pop()
                self.files = os.listdir(
                self.index = 0
                # got a filename
                fullname = os.path.join(, file)
                if os.path.isdir(fullname) and not os.path.islink(fullname):
                    # it's a directory; save it for later
                return fullname

for file in DirectoryWalker("."):
    print file
    if something(file):


when you loop over this object, the __getitem__ function will be
called over and over again, until the object raises an IndexError.

(chances are that Python 2.2 will provide a better iterator inter-
face, which makes this a bit more efficient.  see PEP 234 for more
info [2]).

Cheers /F

1) a function that I have to look up in the reference documentation
everytime I want to use it cannot possibly be called pythonic ;-)


More information about the Python-list mailing list