1. Introduction to the glob Module¶
The glob module provides a function to match file paths using patterns. It’s useful for listing files in directories based on specific patterns.
2. Basic Pattern Matching¶
You can use glob.glob()
to get a list of files that match a specific pattern.
Example:
In [2]:
import glob
# List all .txt files in the current directory
txt_files = glob.glob("*.txt")
print("Text files:", txt_files)
Text files: ['example.txt']
3. Using Wildcards¶
The glob module supports wildcards for pattern matching:
*
matches zero or more characters?
matches a single character[abc]
matches any one of the characters inside the brackets.
Example
In [9]:
# List all files that start with 'data' and end with .csv
csv_files = glob.glob("Intro*.ipynb")
print("CSV files:", csv_files)
# List all files with either 'a', 'b', or 'c' in the name
specific_files = glob.glob("[a-c]*.txt")
print("Specific files:", specific_files)
CSV files: ['Introduction_to_os_module.ipynb', 'Intro_to_glob.ipynb'] Specific files: []
4. Recursive Matching¶
Starting with Python 3.5, you can use the ** pattern to recursively search for files in directories.
In [5]:
# List all .txt files in the current directory and subdirectories
all_txt_files = glob.glob("**/*.txt", recursive=True)
print("All text files (recursively):", all_txt_files)
All text files (recursively): ['example.txt']
5. Combining with Other Modules¶
You can combine glob with other modules for more advanced file handling.
Example:
In [11]:
# List all .py files and print their absolute paths
py_files = glob.glob("*.ipynb")
for file in py_files:
print(os.path.abspath(file))
/Users/ssin4735/Documents/GitHub/GeoTools/FileHandling/Introduction_to_os_module.ipynb /Users/ssin4735/Documents/GitHub/GeoTools/FileHandling/Intro_to_glob.ipynb /Users/ssin4735/Documents/GitHub/GeoTools/FileHandling/Re_module.ipynb