Log in

No account? Create an account

Python Generators - Ben

May. 12th, 2005

02:01 pm - Python Generators

Previous Entry Share Next Entry

Didn't understand the coolness of python generators today. I wanted to generate a list of
files in a directory. Previously I had to write a class and do __iter__ tricks. Now I can
simply do this:

def file_walk(path):
    for dn, ignore, files in os.walk(path):
        for fn in files:
            yield dn + os.sep + fn