← Back to list

Python File Handling with the glob Module: A Comprehensive Guide

Introduction

Mehedi Khan in Django Unleashed · 2024-08-15 02:01 · 69 claps · 2.7 min read
#python #glob #python-glob #python-file-handling #python-programming
Open on Medium ↗
Wiki topics: 💻 · Programming

Python File Handling with the glob Module: A Comprehensive Guide

Python File Handling with the glob Module

Python File Handling with the glob Module

Introduction

In Python, handling files and directories efficiently is crucial for many tasks, whether you’re working on a small project or a large-scale application. The glob module in Python is a powerful tool that simplifies the process of finding files that match a specific pattern in a directory. In this tutorial, we'll explore how to use the glob module to manage files and directories effectively. By the end of this guide, you'll be equipped with the knowledge to leverage glob for various file-handling tasks in Python.

What is the glob Module?

The glob module is a part of Python's standard library and is used to find all the pathnames matching a specified pattern according to the rules used by the Unix shell. It’s especially useful for searching files with specific extensions, names, or patterns in a directory or across directories.

Why Use the glob Module?

  • Pattern Matching: glob allows you to search for files based on specific patterns, such as file extensions or filename formats.
  • Cross-Platform: It’s compatible across different operating systems, making it a reliable choice for file handling.
  • Ease of Use: With simple syntax and powerful functionality, glob makes file searching and management straightforward.

Getting Started with glob

First, let’s start by importing the glob module:

import glob

Basic Usage of glob

1. Finding Files with a Specific Extension

To find all files with a specific extension, such as .txt, in the current directory:

import glob

# Get all .txt files from current directory
text_files = glob.glob("*.txt")
print(text_files)

This will return a list of all .txt files in the current directory.

2. Searching in Subdirectories

You can search for files in subdirectories using the ** wildcard:

import glob

# Get all .txt files in the current directory and its subdirectories
text_files = glob.glob("**/*.txt", recursive=True)
print(text_files)

Here, recursive=True allows glob to search through all subdirectories as well.

3. Matching Multiple Patterns

If you need to match multiple patterns, such as .txt and .py files, you can do it like this:

import glob

# Match both .txt and .py files
files = glob.glob("*.{txt,py}")
print(files)

This returns list of all .txt and .py files from current directory.

Advanced glob Usage

1. Handling Absolute Paths

You can use glob to find files with absolute paths:

import glob

# Get all .txt files with absolute paths
files = glob.glob("/path/to/directory/*.txt")
print(files)

Replace "/path/to/directory/" this path with the path that you want to search.

2. Filtering by File Size or Date

While glob doesn’t directly filter files by size or date, you can combine it with other Python modules like os or datetime for advanced filtering:

import os
import datetime
import glob

# Get all .txt files modified within the last 7 days
files = [f for f in glob.glob("*.txt") if datetime.datetime.now() - datetime.datetime.fromtimestamp(os.path.getmtime(f)) < datetime.timedelta(days=7)]
print(files)

Common Use Cases

  • Batch Processing Files: Use glob to batch process files that match a pattern, such as resizing images, renaming files, or converting file formats.
  • Directory Cleanup: Automatically find and delete files that match certain criteria, like old log files.
  • Data Loading: Quickly locate and load multiple datasets or configuration files for processing.

Conclusion

The glob module is an essential tool for Python developers who need to manage files and directories efficiently. Whether you’re working on a small script or a large application, glob offers powerful pattern-matching capabilities that can save you time and effort. With this guide, you now have a solid understanding of how to use glob to handle various file operations in Python. Start using glob in your projects today and streamline your file-handling tasks with ease.

Thank you for reading! If you notice any mistakes or have suggestions for improvement, please leave a comment below.

If you enjoyed this post, please click the 👏 button to help others discover it. You can also follow me on:

GitHub | daily.dev | LinkedIn | YouTube

More Library

[embed]Python Edit descriptionmehedi-khan.medium.com


메타데이터
post_id
10b209fcbfbb
slug
python-file-handling-with-the-glob-module-a-comprehensive-guide-10b209fcbfbb
url
https://medium.com/django-unleashed/python-file-handling-with-the-glob-module-a-comprehensive-guide-10b209fcbfbb
canonical_url
https://medium.com/django-unleashed/python-file-handling-with-the-glob-module-a-comprehensive-guide-10b209fcbfbb
author_url
https://medium.com/@mehedi-khan
status
ok
fetched_at
2026-06-17 08:20:12