Search code examples
pythonsubdirectoryos.walk

Get all subdirectories quickly with Python


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


Solution

  • 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)