Good Python generators example?

Edward C. Jones edcjones at erols.com
Sun Apr 20 21:57:30 EDT 2003


Robert Oschler wrote:
> Can someone point me to a nice concise Python 2.2 compatible example of the
> use of generators?

def walk(top, hidden=False):
     """ Walk through a tree of directories"""
     if not hidden and os.path.basename(top)[0] == '.':
         return
     if os.path.exists(top):
         yield top
     if not os.path.isdir(top):
         return
     names = listdir(top)
     if names == None:
         return
     for name in names:
         fullname = os.path.join(top, name)
         for name2 in walk(fullname, hidden):
             yield name2





More information about the Python-list mailing list