Get all subdirs

xtian wilberforce at gmail.com
Sun Aug 29 15:22:41 CEST 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

Use os.path.isdir():

>>> from os.path import isdir, join
>>> d = "c:/downloads/python"
>>> subdirs = [sub for sub in os.listdir(d) if isdir(join(d, sub))]
>>> print subdirs
['adodbapi-2.0.1', 'pyblosxom', 'pygame', 'quasi_08']

Cheers,
xtian




More information about the Python-list mailing list