1

I have a root directory root_dir and need to write a method subdirs=list_subdirs(root_dir) that would return all subdirectories recursively in a form:

/subdir1
/subdir1/subsubdir1
/subdir1/subsubdir2
/subdir1/subsubdir3
/subdir2
/subdir2/subsubdir1
etc...

Is os.walk() the right way to go?

Danijel
  • 8,198
  • 18
  • 69
  • 133

1 Answers1

3

Seems like this does the trick:

def list_subdirs(in_path):
    subdirs = []
    for x in os.walk(in_path):
        subdirs.append(x[0])
    return subdirs
Danijel
  • 8,198
  • 18
  • 69
  • 133