Back to index

python3.2  3.2.2
Functions | Variables
fnmatch Namespace Reference

Functions

def fnmatch
def _compile_pattern
def filter
def fnmatchcase
def translate

Variables

list __all__ = ["filter", "fnmatch", "fnmatchcase", "translate"]

Detailed Description

Filename matching with shell patterns.

fnmatch(FILENAME, PATTERN) matches according to the local convention.
fnmatchcase(FILENAME, PATTERN) always takes case in account.

The functions operate by translating the pattern into a regular
expression.  They cache the compiled regular expressions for speed.

The function translate(PATTERN) returns a regular expression
corresponding to PATTERN.  (It does not compile it.)

Function Documentation

def fnmatch._compile_pattern (   pat,
  is_bytes = False 
) [private]

Definition at line 39 of file fnmatch.py.

00039 
00040 def _compile_pattern(pat, is_bytes=False):
00041     if is_bytes:
00042         pat_str = str(pat, 'ISO-8859-1')
00043         res_str = translate(pat_str)
00044         res = bytes(res_str, 'ISO-8859-1')
00045     else:
00046         res = translate(pat)
00047     return re.compile(res).match

Here is the call graph for this function:

Here is the caller graph for this function:

def fnmatch.filter (   names,
  pat 
)
Return the subset of the list NAMES that match PAT.

Definition at line 48 of file fnmatch.py.

00048 
00049 def filter(names, pat):
00050     """Return the subset of the list NAMES that match PAT."""
00051     result = []
00052     pat = os.path.normcase(pat)
00053     match = _compile_pattern(pat, isinstance(pat, bytes))
00054     if os.path is posixpath:
00055         # normcase on posix is NOP. Optimize it away from the loop.
00056         for name in names:
00057             if match(name):
00058                 result.append(name)
00059     else:
00060         for name in names:
00061             if match(os.path.normcase(name)):
00062                 result.append(name)
00063     return result

Here is the call graph for this function:

Here is the caller graph for this function:

def fnmatch.fnmatch (   name,
  pat 
)
Test whether FILENAME matches PATTERN.

Patterns are Unix shell style:

*       matches everything
?       matches any single character
[seq]   matches any character in seq
[!seq]  matches any char not in seq

An initial period in FILENAME is not special.
Both FILENAME and PATTERN are first case-normalized
if the operating system requires it.
If you don't want this, use fnmatchcase(FILENAME, PATTERN).

Definition at line 19 of file fnmatch.py.

00019 
00020 def fnmatch(name, pat):
00021     """Test whether FILENAME matches PATTERN.
00022 
00023     Patterns are Unix shell style:
00024 
00025     *       matches everything
00026     ?       matches any single character
00027     [seq]   matches any character in seq
00028     [!seq]  matches any char not in seq
00029 
00030     An initial period in FILENAME is not special.
00031     Both FILENAME and PATTERN are first case-normalized
00032     if the operating system requires it.
00033     If you don't want this, use fnmatchcase(FILENAME, PATTERN).
00034     """
00035     name = os.path.normcase(name)
00036     pat = os.path.normcase(pat)
00037     return fnmatchcase(name, pat)
00038 
@functools.lru_cache(maxsize=250)

Here is the call graph for this function:

Here is the caller graph for this function:

def fnmatch.fnmatchcase (   name,
  pat 
)
Test whether FILENAME matches PATTERN, including case.

This is a version of fnmatch() which doesn't case-normalize
its arguments.

Definition at line 64 of file fnmatch.py.

00064 
00065 def fnmatchcase(name, pat):
00066     """Test whether FILENAME matches PATTERN, including case.
00067 
00068     This is a version of fnmatch() which doesn't case-normalize
00069     its arguments.
00070     """
00071     match = _compile_pattern(pat, isinstance(pat, bytes))
00072     return match(name) is not None
00073 

Here is the call graph for this function:

Here is the caller graph for this function:

def fnmatch.translate (   pat)
Translate a shell PATTERN to a regular expression.

There is no way to quote meta-characters.

Definition at line 74 of file fnmatch.py.

00074 
00075 def translate(pat):
00076     """Translate a shell PATTERN to a regular expression.
00077 
00078     There is no way to quote meta-characters.
00079     """
00080 
00081     i, n = 0, len(pat)
00082     res = ''
00083     while i < n:
00084         c = pat[i]
00085         i = i+1
00086         if c == '*':
00087             res = res + '.*'
00088         elif c == '?':
00089             res = res + '.'
00090         elif c == '[':
00091             j = i
00092             if j < n and pat[j] == '!':
00093                 j = j+1
00094             if j < n and pat[j] == ']':
00095                 j = j+1
00096             while j < n and pat[j] != ']':
00097                 j = j+1
00098             if j >= n:
00099                 res = res + '\\['
00100             else:
00101                 stuff = pat[i:j].replace('\\','\\\\')
00102                 i = j+1
00103                 if stuff[0] == '!':
00104                     stuff = '^' + stuff[1:]
00105                 elif stuff[0] == '^':
00106                     stuff = '\\' + stuff
00107                 res = '%s[%s]' % (res, stuff)
00108         else:
00109             res = res + re.escape(c)
00110     return res + '\Z(?ms)'

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

list fnmatch.__all__ = ["filter", "fnmatch", "fnmatchcase", "translate"]

Definition at line 17 of file fnmatch.py.