Fredrik Lundh effbot at
Wed May 10 09:40:40 CEST 2000

Douglas Partridge <dpartrid at> wrote:
> Is there a recursive glob?

no, but you can easily roll your own using os.path.walk
and the fnmatch module.

or you can use this iterator class, adapted from an example
in the eff-bot library guide (see below):

# adapted from

import os
import fnmatch

class GlobDirectoryWalker:
    # a forward iterator that traverses a directory tree

    def __init__(self, directory, pattern="*"):
        self.stack = [directory]
        self.pattern = pattern
        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
       = 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):
                if fnmatch.fnmatch(file, self.pattern):
                    return fullname

for file in GlobDirectoryWalker(".", "*.py"):
    print file


<!-- (the eff-bot guide to) the standard python library:

More information about the Python-list mailing list