0

I know the question of how to list all sub-directories in a given directories is answered in this question from 2011. It includes this accepted solution:

subdirs = [x[0] for x in os.walk(dirToSearch)]

That works fine when there are only a few files in the directory. However I am trying to use this on folders that contain thousands of files, and os.walk is apparently iterating over all of them, meaning it takes a really long time to run. Is there a way to do this (identify all subdirectories) without getting bogged down by the files? An alternative to os.walk that ignores files?

I'm trying to do this on a Windows network directory.

Thanks, Alex

Alex S
  • 4,726
  • 7
  • 39
  • 67

1 Answers1

1

You can use pathlib for this.

This will get all immediate subdirectories:

from pathlib import Path

p = Path('.')
subdirs = [x for x in p.iterdir() if x.is_dir()]

This will get all nested subdirectories:

for subdir in p.glob('**/'):
     print(subdir.name)
Kurt Kline
  • 1,724
  • 1
  • 10
  • 23
  • Perfect! Thanks a lot. – Alex S May 12 '20 at 18:55
  • No problem. I updated my answer, in case you needed both immediate subdirectories, or all nested subdirectories. – Kurt Kline May 12 '20 at 18:57
  • Hmm I spoke too soon. Although your first method works great for looking in immediate subdirectories, your method to get all nested subdirectories doesn't appear to be any faster than other methods. Any ideas? – Alex S May 12 '20 at 22:09