glob2

0.7 · deprecated · verified Sun Apr 12

glob2 is an extended version of Python's built-in `glob` module, designed to offer enhanced globbing capabilities. Key features include the ability to capture patterns matched by glob expressions and a recursive `**` wildcard syntax, akin to bash's globstar. The current version is 0.7. The project has seen no active development since 2019, as many of its core functionalities, particularly recursive globbing, have been integrated into Python's standard `glob` module since Python 3.5.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates `glob2`'s main features: recursive globbing using `**` to find all header files within a directory and its subdirectories, and capturing parts of filenames using `with_matches=True`.

import glob2
import os

# Create some dummy files and directories for demonstration
os.makedirs('src/media/mp3', exist_ok=True)
with open('src/fs.h', 'w') as f: f.write('// fs.h')
with open('src/media/mp3.h', 'w') as f: f.write('// mp3.h')
with open('src/media/mp3/frame.h', 'w') as f: f.write('// frame.h')
with open('src/main.py', 'w') as f: f.write('# main.py')

# Example 1: Recursive globbing with '**'
print('All header files:')
all_header_files = glob2.glob('src/**/*.h')
for f in all_header_files:
    print(f)

# Example 2: Capturing patterns (with_matches=True)
# This example requires a pattern that defines capture groups, e.g., project-(.*).zip
# For demonstration, let's assume we have files like 'binaries/project-v1.0.zip'
os.makedirs('binaries', exist_ok=True)
with open('binaries/project-v1.0.zip', 'w') as f: f.write('zip content')
with open('binaries/project-v2.1.zip', 'w') as f: f.write('zip content')

print('\nProject versions (captured patterns):')
for filename, (version,) in glob2.iglob('./binaries/project-*.zip', with_matches=True):
    print(f'{filename}, version: {version}')

# Clean up dummy files and directories
import shutil
shutil.rmtree('src', ignore_errors=True)
shutil.rmtree('binaries', ignore_errors=True)

view raw JSON →