Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

How To Guides
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions
Privacy Policy




The glob and fnmatch Modules

The glob module adds a necessary Unix shell capability to Windows programmers. The glob module includes the following function

glob.glob( wildcard ) → list

Return a list of filenames that match the given wild-card pattern. The fnmatch module is used for the wild-card pattern matching.

A common use for glob is something like the following under Windows.

import glob, sys
for arg in sys.argv[1:]:
    for f in glob.glob(arg):
        process( f )

This makes Windows programs process command line arguments somewhat like Unix programs. Each argument is passed to glob.glob to expand any patterns into a list of matching files. If the argument is not a wild-card pattern, glob simply returns a list containing this one file name.

The fnmatch module has the algorithm for actually matching a wild-card pattern against specific file names. This module implements the Unix shell wild-card rules. These are not the same as the more sophisticated regular expression rules. The module contains the following function:

fnmatch.fnmatch ( file , pattern ) → boolean

Return True if the file matches the pattern.

The patterns use * to match any number of characters, ? to match any single character. [letters] matches any of these letters, and [!letters] matches any letter that is not in the given set of letters.

import fnmatch




  Published under the terms of the Open Publication License Design by Interspire