Last modified: Mar 19, 2025 By Alexander Williams

Python Pathlib rglob() Explained

Python's pathlib module is a powerful tool for handling file paths. One of its most useful methods is rglob(). This method allows you to recursively search for files and directories. It is similar to glob(), but it searches through all subdirectories.

In this article, we will explore how to use rglob() effectively. We will also provide examples and outputs to help you understand its functionality.

What is pathlib.rglob()?

The rglob() method is part of the pathlib.Path class. It is used to search for files and directories recursively. This means it will search through all subdirectories of the given path.

This method is particularly useful when you need to find all files of a certain type in a directory and its subdirectories. For example, you might want to find all .txt files in a project folder.

How to Use pathlib.rglob()

Using rglob() is straightforward. You start by creating a Path object. Then, you call the rglob() method with a pattern. The pattern can include wildcards like * and ?.

Here is a simple example:


from pathlib import Path

# Create a Path object
path = Path('my_project')

# Use rglob to find all .txt files
txt_files = list(path.rglob('*.txt'))

# Print the results
for file in txt_files:
    print(file)

In this example, rglob('*.txt') will search for all .txt files in the my_project directory and its subdirectories. The results are stored in a list and then printed.

Example with Output

Let's consider a more detailed example. Suppose you have a directory structure like this:


my_project/
├── file1.txt
├── file2.txt
├── subdir1/
│   ├── file3.txt
│   └── file4.log
└── subdir2/
    └── file5.txt

Now, let's use rglob() to find all .txt files:


from pathlib import Path

# Create a Path object
path = Path('my_project')

# Use rglob to find all .txt files
txt_files = list(path.rglob('*.txt'))

# Print the results
for file in txt_files:
    print(file)

The output will be:


my_project/file1.txt
my_project/file2.txt
my_project/subdir1/file3.txt
my_project/subdir2/file5.txt

As you can see, rglob() successfully found all .txt files, including those in subdirectories.

Combining rglob() with Other Methods

You can combine rglob() with other pathlib methods for more advanced file handling. For example, you can use is_file() to filter out directories.

Here is an example:


from pathlib import Path

# Create a Path object
path = Path('my_project')

# Use rglob to find all .txt files and filter out directories
txt_files = [file for file in path.rglob('*.txt') if file.is_file()]

# Print the results
for file in txt_files:
    print(file)

This code will produce the same output as before, but it ensures that only files are included in the results.

Conclusion

The rglob() method in Python's pathlib module is a powerful tool for recursively searching for files and directories. It is easy to use and can be combined with other methods for more advanced file handling.

By understanding how to use rglob(), you can efficiently manage and search through complex directory structures. This makes it an essential tool for any Python developer working with file systems.

For more information on related methods, check out our articles on Python Pathlib glob() Explained and Python Pathlib is_file() Explained.