loops -> list/generator comprehensions

jamesthiele.usenet at gmail.com jamesthiele.usenet at gmail.com
Sun Feb 6 14:28:37 EST 2005


I wrote this little piece of code to get a list of relative paths of
all files in or below the current directory (*NIX):

    walkList  = [(x[0], x[2]) for x in os.walk(".")]
    filenames = []
    for dir, files in walkList:
        filenames.extend(["/".join([dir, f]) for f in files])

It works fine, I don't need to change it, but I know there is a one
liner list/generator comprehension to do this - I'm just not well
enough versed in comprehensions to figure it out. Can someone please
show me what it is?

Even better, is there a generalized way to transform simple loops into
comprehensions that someone can point me to?

james




More information about the Python-list mailing list