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

</>
Copy
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:

  1. We import the os module to interact with the filesystem.
  2. The variable folder_path stores the path of the folder where we want to list directories.
  3. The os.listdir(folder_path) method retrieves all items (files and folders) in the specified directory.
  4. We use list comprehension with os.path.isdir() to check if each item is a directory.
  5. 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

</>
Copy
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:

  1. We import the os module.
  2. The folder_path variable holds the directory we want to scan.
  3. The os.scandir(folder_path) function returns an iterator with detailed information about each item.
  4. We use list comprehension with entry.is_dir() to check if an item is a directory.
  5. 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

</>
Copy
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:

  1. We import Path from the pathlib module.
  2. The folder_path is created as a Path object.
  3. The iterdir() method returns an iterator over the directory contents.
  4. We use list comprehension with item.is_dir() to filter directories.
  5. 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

</>
Copy
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:

  1. We import the glob module.
  2. The folder_path variable specifies a wildcard pattern that selects directories.
  3. The glob.glob(folder_path) returns all matching directories.
  4. We use list comprehension to remove trailing slashes using rstrip("\\/").
  5. 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:

  1. os.listdir() – Simple but requires filtering.
  2. os.scandir() – More efficient than os.listdir().
  3. pathlib.Path.iterdir() – Modern and object-oriented.
  4. glob.glob() – Uses wildcard patterns for directory selection.

The pathlib approach is recommended for newer Python versions.