glob vs rglob with example
- User
glob vs rglob with example
- Assistant
glob vs rglob with example
The terms glob and rglob are commonly used in programming, particularly in Python, to perform pattern matching on file paths. They are methods provided by modules like glob and pathlib to search for files and directories that match a specified pattern.
Here's a detailed explanation of glob vs. rglob, along with examples to illustrate their differences.
glob and rglobglobglob stands for "global" and is used to retrieve files/pathnames matching a specified pattern.rglobr in rglob stands for "recursive glob".glob ModuleThe built-in glob module in Python provides functions to work with Unix shell-style wildcards.
glob.globimport glob
# List all .txt files in the current directory
txt_files = glob.glob('*.txt')
print(txt_files)
.txt in the current directory.glob.globTo perform recursive searches with glob.glob, you need to use the ** pattern and set recursive=True.
import glob
# List all .txt files in the current directory and subdirectories
txt_files_recursive = glob.glob('**/*.txt', recursive=True)
print(txt_files_recursive)
** pattern tells glob to include all subdirectories recursively.** is supported in Python 3.5 and above.pathlib ModuleThe pathlib module introduced in Python 3.4 provides an object-oriented approach to handle filesystem paths.
Path.globfrom pathlib import Path
# Create a Path object for the current directory
p = Path('.')
# List all .txt files in the current directory
txt_files = list(p.glob('*.txt'))
print(txt_files)
Path.glob('*.txt') searches for .txt files in the directory represented by p.Path.glob and Path.rglobPath.glob with **from pathlib import Path
# List all .txt files in the current directory and subdirectories using '**'
txt_files_recursive = list(p.glob('**/*.txt'))
print(txt_files_recursive)
'**/*.txt' tells glob to include all subdirectories.Path.rglobfrom pathlib import Path
# List all .txt files in the current directory and subdirectories using rglob
txt_files_recursive_rglob = list(p.rglob('*.txt'))
print(txt_files_recursive_rglob)
Path.rglob('*.txt') is a shortcut for Path.glob('**/*.txt').glob vs. rglobglob: Current directory (or specified directory).rglob: Current directory and all subdirectories (recursively).*, ?, and [].glob module: Use glob.glob() with appropriate patterns.pathlib module: Use Path.glob() or Path.rglob().Consider the following directory structure:
project/
├── main.py
├── data.txt
├── results/
│ ├── output1.txt
│ └── logs/
│ └── log1.txt
└── docs/
└── readme.md
glob.globimport glob
# Non-recursive search
print(glob.glob('*.txt')) # Outputs: ['data.txt']
# Recursive search
print(glob.glob('**/*.txt', recursive=True)) # Outputs: ['data.txt', 'results/output1.txt', 'results/logs/log1.txt']
Path.globfrom pathlib import Path
# Create a Path object for 'project' directory
p = Path('project')
# Non-recursive search
print(list(p.glob('*.txt'))) # Outputs: [PosixPath('project/data.txt')]
# Recursive search
print(list(p.glob('**/*.txt'))) # Outputs: [PosixPath('project/data.txt'), PosixPath('project/results/output1.txt'), PosixPath('project/results/logs/log1.txt')]
Path.rglobfrom pathlib import Path
# Create a Path object for 'project' directory
p = Path('project')
# Recursive search with rglob
print(list(p.rglob('*.txt'))) # Outputs: [PosixPath('project/data.txt'), PosixPath('project/results/output1.txt'), PosixPath('project/results/logs/log1.txt')]
glob when you want to match files in a specific directory without descending into subdirectories.rglob when you need to search recursively through all subdirectories.pathlib module, rglob(pattern) is equivalent to glob('**/' + pattern).recursive=True when using recursive patterns with the glob module.Wildcards:
* matches any number of characters (including none).? matches a single character.[] matches any one character inside the brackets.Performance Consideration:
Cross-Platform Compatibility:
pathlib module is generally more cross-platform friendly than the glob module.pathlib handles path separators (/ vs. \) internally based on the operating system.By understanding the differences between glob and rglob, you can effectively search and manipulate files in your Python programs, whether you need to search a single directory or traverse an entire directory tree.
Not yet included in a published release.