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

Paul Jackson pj at sgi.com
Wed Mar 28 15:30:58 EST 2001


Remco suggested:
|> 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 doubt this will work.  The condition:

    if os.path.isfile(f)

will fail to test the right path.  You have to test "a/b/c/f",
not "f".  Only by first changing directory to "a/b/c" would
the above work.
-- 
                          I won't rest till it's the best ...
                          Manager, Linux System Software
                          Paul Jackson <pj at sgi.com> 1.650.933.1373



More information about the Python-list mailing list