Getting all the *files* from a directory -- A better way??

Remco Gerlich scarblac at
Wed Mar 28 13:44:39 CEST 2001

Paul Jackson <pj at> wrote in comp.lang.python:
 > Close - better to use os.path.join (more portable across
> operating environments with different path component
> separator characters, such as '\' in DOS/Windows).
> Try:
>     dir="a/b/c"
>     files = filter(
> 	os.path.isfile,
> 	[os.path.join(dir, f) for f in os.listdir(dir)]
>     )

Or even

files = [os.path.join(dir, f) for f in os.listdir(dir)
                              if os.path.isfile(f)]

If you do the map() with a list comprehension, why not the filter() as well.

I don't know if this is the Pythonic way to do it, but it comes pretty close
to the Haskellic way :).

Remco Gerlich

More information about the Python-list mailing list