Get all subdirs
Tim Peters
tim.peters at gmail.com
Sun Aug 29 11:17:11 EDT 2004
[Florian Lindner]
> 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.
os.walk() is a generator -- it doesn't descend into anything unless
you resume it. That's the usual case, but you don't need to resume
it.
def subdirs(dir):
"Return list of the subdirectories of dir."
for root, dirs, files in os.walk(dir):
return dirs
works fine, or, perhaps more obscurely,
def subdirs(dir):
"Return list of the subdirectories of dir."
return os.walk(dir).next()[1]
More information about the Python-list
mailing list