Get all subdirs

George Yoshida ml at dynkin.com
Sun Aug 29 09:25:58 EDT 2004


Florian Lindner wrote:
> Hello,
> how can I get all subdirectories of a given directory. os.listdir(dir)
> doesn't differentiate between directories and files, os.walk seems to me a
> bit overkill since it also descends in the subdirs.
> Thx,
> Florian

What about testing return values of os.listdir with os.path.isdir?
e.g.,

   filter(os.path.isdir, os.listdir(dir))

or if you don't like high-order functions,

   [filename for filename in os.listdir(dir) if os.path.isdir(filename)]

--
George



More information about the Python-list mailing list