Getting all the *files* from a directory -- A better way??
Remco Gerlich
scarblac at pino.selwerd.nl
Wed Mar 28 06:44:39 EST 2001
Paul Jackson <pj at sgi.com> 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
dir="a/b/c"
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