Pathlib List Directories: Complete Guide (2026)

Pathlib List Directories: Complete Guide (2026)
sjorsfest
sjorsfest

Startup engineer with 8+ years of experience building and shipping products. Now an independent builder creating tools for small companies and indie makers, including Donkey Directories: A chrome extension which helps builders automatically fill in directory submission forms

7 min read

Modern Python development favors the object-oriented approach of the pathlib module over the older os module. This guide provides a comprehensive walkthrough for using pathlib to list directories, filter folder structures, and manage file system paths efficiently. Whether you are a Python developer or a data scientist, mastering these methods will streamline your workflow for tasks ranging from local project organization to complex directory structure analysis on platforms like Product Hunt. This article covers iterdir, rglob, and glob methods with practical examples to help you transition from legacy os.path calls to modern Pythonic standards.

Introduction to pathlib for Directory Listing

Python developers often face the repetitive pain of manual file system navigation, similar to how founders struggle with manual directory submissions. Using legacy methods like os.listdir or os.walk often leads to brittle code that handles paths as mere strings, increasing the risk of errors when managing complex directory structures. Pathlib solves this by treating paths as objects, providing a cleaner API for common tasks.

Whether you are configuring additional include directories for a C++ project in CLion include directories or simply trying to list all directories in a repo, pathlib offers a robust solution. This guide ensures you have the required capabilities to build automated tools, such as scripts for a product hunt developer or data pipelines that require precise folder filtering.

Getting Started with pathlib.Path

To effectively use pathlib list directories, you must first understand the Path object. Unlike strings, Path objects come with built-in methods for checking permissions, joining segments, and performing scans.

from pathlib import Path

# Create a Path object for the current directory
base_path = Path('.')

# Resolve to an absolute path to avoid ambiguity
absolute_path = base_path.resolve()
print(f'Working in: {absolute_path}')

When building tools like a knowt product hunt tracker, you rely on these Path objects to navigate nested folders without manually concatenating strings with slashes. The auto-merge of path segments via the slash operator (/) is a hallmark of this module.

Basic Directory Listing with iterdir()

The most direct way to list all directories in a specific folder is using the iterdir() method. This method creates an iterator that yields Path objects for every entry in the directory. To isolate folders, you combine iterdir() with the is_dir() check.

from pathlib import Path

# Target directory
p = Path('./my_project')

# List only directories in the current level
# This is a non-recursive operation
dirs = [entry for entry in p.iterdir() if entry.is_dir()]

for d in dirs:
    print(f'Found directory: {d.name}')

# Output Example:
# Found directory: src
# Found directory: tests
# Found directory: docs

This method is preferred for a category view of a root folder where you do not need to dive into sub-folders. It is memory efficient, which is vital when scanning directories structure at scale.

Step-by-Step Implementation

  1. 1Import Path from pathlib: This is your primary tool for all file system operations.
  2. 2Define your entry point: Use Path.cwd() or a specific string path to initialize your search.
  3. 3Choose your depth: Use iterdir() for shallow scans or rglob() for deep recursive searches.
  4. 4Apply filters: Use is_dir() to exclude files and use .name.startswith('.') to skip hidden folders.
  5. 5Handle Exceptions: Wrap your logic in a try-except block to catch PermissionError.

Recursive Directory Listing with rglob()

Recursive directory listing is essential for deep scans. The rglob() method is a shortcut for recursive globbing. It allows you to find all sub-directories regardless of depth. This is particularly useful when you need to run a du hidden directories analysis or locate all 'node_modules' folders in a large project.

from pathlib import Path

# Root of your project
root = Path('./src')

# Recursively find all subdirectories using a pattern
# '*' matches all, but we filter for directories
for folder in root.rglob('*'):
    if folder.is_dir():
        print(f'Nested folder: {folder}')

# Output Example:
# Nested folder: src/utils
# Nested folder: src/utils/helpers
# Nested folder: src/api/v1

Using rglob helps Python developers scan entire project trees for configuration files or log folders without writing complex recursive functions manually.

Pattern Matching with glob() for Selective Listing

The glob() method is perfect for selective listing based on name patterns. For example, if you only want to find folders that start with 'test' or end with 'logs', glob patterns make this easy. This is similar to using a content-aware fill tool for your file system logic, targeting only what you need.

from pathlib import Path

p = Path('./build')

# Find all directories that start with 'v' (e.g., v1, v2)
# The pattern '**/' can be used for recursion if needed
for match in p.glob('v*/'):
    if match.is_dir():
        print(f'Matched version folder: {match.name}')

This selective approach is often used in client track applications to find specific user data folders quickly without scanning the whole drive.

Pathlib Method Comparison Table

MethodRecursionPattern SupportBest Use Case
iterdir()NoNoneSimple, single-level listing
glob()OptionalWildcards (*, ?)Selective filtering by name
rglob()YesRecursive Wildcards (**)Deep scans and tree traversal

Sorting Directory Listings

Beyond just listing, you often need to transform or organize the output. Python's built-in sorted() function allows you to order your directory list by various metadata.

from pathlib import Path

p = Path('./logs')

# Sort directories by modification time (newest first)
# Using stat().st_mtime for the sort key
sorted_dirs = sorted(
    [d for d in p.iterdir() if d.is_dir()],
    key=lambda x: x.stat().st_mtime,
    reverse=True
)

for d in sorted_dirs:
    print(f'Recent activity in: {d.name}')

Sorting is critical when monitoring abrt-auto-reporting enabled systems to find the latest crash logs or when reviewing recent submissions for a shyp product hunt tool.

Real-World Use Cases and Examples

Let's look at more complex scenarios where pathlib excels.

### Use Case: Project Tree Viewer
You can build a simple tree viewer to visualize your directories structure. This helps when setting up cp include directories or checking project organization.

def list_tree(directory, indent=""):
    for path in sorted(directory.iterdir()):
        if path.is_dir():
            print(f"{indent}└── {path.name}/")
            list_tree(path, indent + "    ")

list_tree(Path('./src'))

### Use Case: Finding Heavy Folders
Combine pathlib with basic math to find directories that might be taking up too much space, similar to how a fill command hollow might show volume.

from pathlib import Path

def get_dir_size(path):
    return sum(f.stat().st_size for f in path.rglob('*') if f.is_file())

root = Path('./data')
heavy_folders = {d.name: get_dir_size(d) for d in root.iterdir() if d.is_dir()}

Performance Comparison: pathlib vs os module

While pathlib is modern, how does it stack up against the old school os methods?

FeaturePathlib (Modern)OS Module (Legacy)
Return TypePath ObjectsStrings
ReadabilityHigh (Object-oriented)Low (String-heavy)
PerformanceSlightly slower on massive treesFaster for simple string lists
RecursionBuilt-in (rglob)Requires os.walk

For 99% of tasks, such as creating a product hunt guide or automating a knowt product hunt sequence, pathlib is the winner due to developer speed. However, if performance is the absolute priority on a file system with millions of files, os.listdir may offer a slight edge. Regardless, for modern readability and safety (like ensuring you fill missing brackets in your logic), pathlib is the professional choice.

Error Handling Best Practices

File system operations are prone to errors. You might run into a PermissionError when trying to list root-level folders or a FileNotFoundError if a path is moved during execution.

from pathlib import Path

def safe_list_dir(path_str):
    try:
        p = Path(path_str)
        return [d.name for d in p.iterdir() if d.is_dir()]
    except PermissionError:
        print(f"Permission denied: {path_str}")
        return []
    except FileNotFoundError:
        print(f"Path not found: {path_str}")
        return []

# Usage
my_dirs = safe_list_dir('/usr/local/bin')

Always ensure your scripts handle these cases, especially when scanning sensitive system areas where nftables list all rules might be stored or where system logs reside.

FAQ: Pathlib Directory Listing Questions

What is the difference between iterdir(), glob(), and rglob() in pathlib?+

iterdir() is for a shallow scan of the immediate directory. glob() uses patterns for selective matches. rglob() is a recursive glob that searches all subfolders.

How do I filter only directories (not files) with pathlib?+

You can filter using the .is_dir() method within a list comprehension. For example: [d for d in p.iterdir() if d.is_dir()].

How do I list directories recursively with pathlib?+

Use the rglob('*') method on a Path object. It will yield every file and directory in the entire tree. Combine with is_dir() to narrow it down to folders only.

How do I sort directory listings by name, size, or modification date?+

Pathlib treats paths as objects, so you can pass them to the built-in sorted() function. To sort by specific attributes, use a lambda function as the key, such as key=lambda x: x.stat().st_size.

Conclusion and Quick Reference

Mastering pathlib list directories is a foundational skill for any Python developer in 2026. By moving away from the legacy os module and embracing the object-oriented Path class, you ensure your code is readable, maintainable, and robust. Whether you are automating a directory submission for a shyp product hunt launch or organizing complex local project data, methods like iterdir and rglob are your best tools. For those ready to move from script development to product launching, consider using Donkey Directories to manage your visibility and reach your first customers efficiently.

Sources and Further Reading