Python glob Module: How to Install and Use with Advanced Examples

Python glob Module

Module Introduction

The glob module in Python is part of the standard library, and it is used to retrieve files/pathnames matching a specified pattern. This module provides a convenient way to search for files within directories, allowing for simple wildcard usage commonly found in shell commands. It is available in Python 3 and does not require any additional installation as it is included with the standard distribution.

Application Scenarios

The glob module is particularly useful in various scenarios, including:

  • File Management: Automating the search and manipulation of files in directories based on patterns.
  • Data Processing: Finding specific datasets or files needed for analysis efficiently.
  • Web Scrapping: Gathering files based on naming conventions downloaded from websites.

Through these applications, users can streamline their workflow and save time when dealing with multiple files.

Installation Instructions

Since glob is part of the Python standard library, it is included by default in Python 3 installations. To ensure you are using Python 3, you can check your version by running:

1
python --version  # This should return your current Python version

If Python 3 is installed, you can start using the glob module directly without any additional installations.

Usage Examples

1. Example 1: List All .txt Files in a Directory

1
2
3
4
5
6
7
import glob  # Importing the glob module to use its functionalities

# Use glob to find all .txt files in the current directory
txt_files = glob.glob('*.txt') # The pattern '*.txt' matches all text files

# Printing the found txt files
print("Text files found:", txt_files) # Output the list of found text files

In this example, we aim to find all .txt files in the current working directory, allowing for quick access to text documents.

2. Example 2: Search for Specific Patterns in Subdirectories

1
2
3
4
5
6
7
import glob  # Importing the glob module

# Searching for all Python files recursively in all subdirectories
py_files = glob.glob('**/*.py', recursive=True) # '**/*.py' looks for Python files in all subfolders

# Printing the list of found Python files
print("Python files in subdirectories:", py_files) # Show the found Python files

Here, the usage of **/*.py allows for searching not just in the current directory but also in all its subdirectories for Python files, making it very effective for larger projects.

3. Example 3: Filtering Files by Date Modified

1
2
3
4
5
6
7
8
9
10
11
12
13
import glob  # Importing glob for file matching
from datetime import datetime # Importing datetime to work with file timestamps
import os # Importing os to access file metadata

# Fetch all files in the current directory
files = glob.glob('*') # Match any file in the current directory

# Filter files modified within the last 7 days
recent_files = [f for f in files if
(datetime.now() - datetime.fromtimestamp(os.path.getmtime(f))).days < 7]

# Printing filtered recent files
print("Files modified in the last 7 days:", recent_files) # Output the list of recent files

In this example, we demonstrate how to combine glob with os and datetime to filter files that were modified recently based on their last modification time. This can be useful for tracking recent changes to files.


I strongly encourage everyone to follow my blog, EVZS Blog. It provides comprehensive tutorials on all Python standard library modules, making it easier for you to find and learn about specific functionalities. By following along, you’ll gain valuable insights and build a solid foundation in Python programming, enhancing your skills and making your coding journey smoother. Don’t miss out on the opportunity to engage with a wealth of knowledge at your fingertips!

SOFTWARE VERSION MAY CHANG

If this document is no longer applicable or incorrect, please leave a message or contact me for update. Let's create a good learning atmosphere together. Thank you for your support! - Travis Tang