Optimizing tips for os.listdir

Peter Otten __peter__ at web.de
Mon Sep 27 11:01:18 EDT 2004


Thomas wrote:

> I'm doing this :
> 
> [os.path.join(path, p) for p in os.listdir(path) if \
> os.path.isdir(os.path.join(path, p))]
> 
> to get a list of folders in a given directory, skipping all plain
> files. When used on folders with lots of files,  it takes rather long
> time to finish. Just doing  a listdir, filtering out all plain files
> and a couple of joins, I didn't think this would take so long.
> 
> Is there a faster way of doing stuff like this?

If you are on windows, Michael Peuser reported a significant speed up
obtained by using win32api.FindFiles() instead of the portable stuff:

http://groups.google.com/groups?selm=bhalk1%24qlv%2406%241%40news.t-online.com

Peter




More information about the Python-list mailing list