List all subdirectories in a directory in Python
This post will discuss how to list all subdirectories in a directory in Python.
1. Using os.listdir() function
A simple solution to list all subdirectories in a directory is using the os.listdir() function. However, this returns the list of all files and subdirectories in the root directory. You can filter the returned list using the os.path.isdir() function to list only the subdirectories.
|
1 2 3 4 5 6 7 8 |
import os rootdir = 'path/to/dir' for file in os.listdir(rootdir): d = os.path.join(rootdir, file) if os.path.isdir(d): print(d) |
You can easily extend the solution to search within subdirectories as well, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
import os def listdirs(rootdir): for file in os.listdir(rootdir): d = os.path.join(rootdir, file) if os.path.isdir(d): print(d) listdirs(d) rootdir = 'path/to/dir' listdirs(rootdir) |
2. Using os.scandir() function
With Python 3.5, you can use the os.scandir() function, which offers significantly better performance over os.listdir(). It returns directory entries along with file attribute information. To filter the returned entries to exclude files, call the is_dir() function, which returns True if the current entry is a directory or a symbolic link pointing to a directory.
|
1 2 3 4 5 6 7 |
import os rootdir = 'path/to/dir' for it in os.scandir(rootdir): if it.is_dir(): print(it.path) |
You can easily make the above code recursive to enable search within subdirectories:
|
1 2 3 4 5 6 7 8 9 10 11 |
import os def listdirs(rootdir): for it in os.scandir(rootdir): if it.is_dir(): print(it.path) listdirs(it) rootdir = 'path/to/dir' listdirs(rootdir) |
3. Using pathlib module
You can also use the pathlib module with Python 3.4 to list all subdirectories in a directory. The idea is to call the Path.iterdir() function, yielding path objects of the directory contents. You can filter the returned objects for directories or a symbolic link pointing to a directory, use the Path.is_dir()() function.
|
1 2 3 4 5 6 7 |
from pathlib import Path rootdir = 'path/to/dir' for path in Path(rootdir).iterdir(): if path.is_dir(): print(path) |
Here’s the recursive version, which also searches within the subdirectories:
|
1 2 3 4 5 6 7 8 9 10 11 |
from pathlib import Path def listdirs(rootdir): for path in Path(rootdir).iterdir(): if path.is_dir(): print(path) listdirs(path) rootdir = 'path/to/dir' listdirs(rootdir) |
4. Using os.walk() function
To search in subdirectories, consider using the os.walk() function. It recursively yields a 3-tuple (dirpath, dirnames, filenames), where dirpath is the path to the current directory, dirnames is a list of the names of the subdirectories in the current directory and filenames lists the regular files in the current directory.
|
1 2 3 4 5 6 7 |
import os rootdir = 'path/to/dir' for rootdir, dirs, files in os.walk(rootdir): for subdir in dirs: print(os.path.join(rootdir, subdir)) |
5. Using glob module
Finally, you can use the glob.glob function, which returns an iterator over the list of pathnames that match the specified pattern.
|
1 2 3 4 5 6 |
import glob rootdir = 'path/to/dir' for path in glob.glob(f'{rootdir}/*/'): print(path) |
Python 3.5 extended support for recursive globs using ** to search subdirectories and symbolic links to directories.
|
1 2 3 4 5 6 |
import glob rootdir = 'path/to/dir' for path in glob.glob(f'{rootdir}/*/**/', recursive=True): print(path) |
That’s all about listing all subdirectories in a directory in Python.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)