Friday, October 10, 2008

Example usage of the glob module

The python glob module in the standard library is a simple, useful way of expanding wildcard matches to filenames. It dovetails nicely with the os, os.path, and shutil modules.

Example usage:

import os, glob
TEMP_DIR = '/tmp/whatever'
for file in glob.glob(os.path.join(TEMP_DIR, '*.tmp')):
os.remove(file)

The glob module contains just two functions: glob() and its case-insensitive generator-returning equivalent, iglob().

No comments: