Last modified: Mar 19, 2025 By Alexander Williams

Python Pathlib glob() Explained

Python's pathlib module simplifies file and directory operations. One of its powerful methods is glob(). It helps in pattern matching for file paths.

This article will explain how to use pathlib.glob() effectively. We'll cover its syntax, examples, and practical use cases.

What is pathlib.glob()?

The glob() method is used to find files and directories matching a specific pattern. It returns a generator of matching paths.

This method is part of the Path class in the pathlib module. It is a modern alternative to the older os and glob modules.

Syntax of pathlib.glob()

The syntax for glob() is straightforward. It takes a pattern as an argument and returns matching paths.


from pathlib import Path

# Example syntax
matching_files = Path('directory_path').glob('pattern')
    

Here, directory_path is the path to the directory. pattern is the file pattern to match.

Basic Example of pathlib.glob()

Let's start with a simple example. Suppose you want to find all .txt files in a directory.


from pathlib import Path

# Find all .txt files in the current directory
txt_files = Path('.').glob('*.txt')

# Print the matching files
for file in txt_files:
    print(file)
    

This code will print all .txt files in the current directory. The * in the pattern matches any file name.

Recursive Pattern Matching

You can also search recursively in subdirectories. Use ** in the pattern to match files in all subdirectories.


from pathlib import Path

# Find all .txt files in the current directory and subdirectories
txt_files = Path('.').glob('**/*.txt')

# Print the matching files
for file in txt_files:
    print(file)
    

This code will find all .txt files in the current directory and its subdirectories.

Practical Use Cases

1. Finding Specific File Types: Use glob() to find files of a specific type, like images or documents.

2. Organizing Files: Automate file organization by moving or renaming files based on patterns.

3. Data Processing: Process multiple files in a directory by iterating over matching paths.

Combining with Other Pathlib Methods

You can combine glob() with other pathlib methods for more advanced operations.

For example, use is_file() to filter out directories. Or use rename() to rename matching files.


from pathlib import Path

# Find all .txt files and rename them
for file in Path('.').glob('*.txt'):
    if file.is_file():
        new_name = file.with_stem(file.stem + '_backup')
        file.rename(new_name)
    

This code renames all .txt files by appending _backup to their names.

Conclusion

The pathlib.glob() method is a powerful tool for file pattern matching. It simplifies working with files and directories in Python.

By combining it with other pathlib methods, you can automate many file-related tasks. Start using glob() today to make your file operations more efficient.

For more on pathlib, check out our guides on Python Pathlib rename() and Python Pathlib is_file().