List All Directories in a Folder in Python
To list all directories in a given folder in Python, you can use the os
or pathlib
module. In this tutorial, we will explore different methods to achieve this with easy-to-understand examples.
Examples to List All Directories
1. Using os.listdir()
to List Directories
We will use the os.listdir()
method to get a list of all items in a folder and filter out only directories.
main.py
import os
# Specify the folder path
folder_path = "/Users/tutorialkart/"
# List all items in the folder and filter only directories
directories = [item for item in os.listdir(folder_path) if os.path.isdir(os.path.join(folder_path, item))]
# Print the directories
print("Directories:", directories)
Explanation:
- We import the
os
module to interact with the filesystem. - The variable
folder_path
stores the path of the folder where we want to list directories. - The
os.listdir(folder_path)
method retrieves all items (files and folders) in the specified directory. - We use list comprehension with
os.path.isdir()
to check if each item is a directory. - The final list
directories
contains only directory names, which we print.
Output:

2. Using os.scandir()
for Better Performance to List All Directories
In this example, we use os.scandir()
for an optimized way to list directories, which is faster than os.listdir()
.
main.py
import os
# Specify the folder path
folder_path = "C:/Users/tutorialkart/Documents"
# Using os.scandir() to list directories
directories = [entry.name for entry in os.scandir(folder_path) if entry.is_dir()]
# Print the directories
print("Directories:", directories)
Explanation:
- We import the
os
module. - The
folder_path
variable holds the directory we want to scan. - The
os.scandir(folder_path)
function returns an iterator with detailed information about each item. - We use list comprehension with
entry.is_dir()
to check if an item is a directory. - The result is stored in
directories
and printed.
Output:
Directories: ['Project1', 'Project2', 'MyFiles', 'Backup']
3. Using pathlib.Path.iterdir()
to List All Directories
In this example, we will use the pathlib
module, which provides an object-oriented way to interact with files and directories.
main.py
from pathlib import Path
# Specify the folder path
folder_path = Path("C:/Users/tutorialkart/Documents")
# Using iterdir() to list directories
directories = [item.name for item in folder_path.iterdir() if item.is_dir()]
# Print the directories
print("Directories:", directories)
Explanation:
- We import
Path
from thepathlib
module. - The
folder_path
is created as aPath
object. - The
iterdir()
method returns an iterator over the directory contents. - We use list comprehension with
item.is_dir()
to filter directories. - The result is stored in
directories
and printed.
Output:
Directories: ['Project1', 'Project2', 'MyFiles', 'Backup']
4. Using glob.glob()
to List Directories
In this example, we will use the glob
module to list only directories inside a given folder.
main.py
import glob
# Specify the folder path
folder_path = "C:/Users/tutorialkart/Documents/*/"
# Using glob to list directories
directories = [dir.rstrip("\\/") for dir in glob.glob(folder_path)]
# Print the directories
print("Directories:", directories)
Explanation:
- We import the
glob
module. - The
folder_path
variable specifies a wildcard pattern that selects directories. - The
glob.glob(folder_path)
returns all matching directories. - We use list comprehension to remove trailing slashes using
rstrip("\\/")
. - The result is stored in
directories
and printed.
Output:
Directories: ['C:/Users/tutorialkart/Documents/Project1', 'C:/Users/tutorialkart/Documents/Project2']
Conclusion
Python provides multiple ways to list directories in a folder:
os.listdir()
– Simple but requires filtering.os.scandir()
– More efficient thanos.listdir()
.pathlib.Path.iterdir()
– Modern and object-oriented.glob.glob()
– Uses wildcard patterns for directory selection.
The pathlib
approach is recommended for newer Python versions.