blob: abf952355051c562f99b56709bf18954e9963e57 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`fnmatch` --- Unix filename pattern matching
2=================================================
3
4.. module:: fnmatch
5 :synopsis: Unix shell style filename pattern matching.
6
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04007**Source code:** :source:`Lib/fnmatch.py`
Georg Brandl116aa622007-08-15 14:28:22 +00008
9.. index:: single: filenames; wildcard expansion
10
11.. index:: module: re
12
Raymond Hettinger4f707fd2011-01-10 19:54:11 +000013--------------
14
Georg Brandl116aa622007-08-15 14:28:22 +000015This module provides support for Unix shell-style wildcards, which are *not* the
16same as regular expressions (which are documented in the :mod:`re` module). The
17special characters used in shell-style wildcards are:
18
19+------------+------------------------------------+
20| Pattern | Meaning |
21+============+====================================+
22| ``*`` | matches everything |
23+------------+------------------------------------+
24| ``?`` | matches any single character |
25+------------+------------------------------------+
26| ``[seq]`` | matches any character in *seq* |
27+------------+------------------------------------+
28| ``[!seq]`` | matches any character not in *seq* |
29+------------+------------------------------------+
30
Ezio Melottia39a22d2012-11-17 17:38:11 +020031For a literal match, wrap the meta-characters in brackets.
32For example, ``'[?]'`` matches the character ``'?'``.
33
Georg Brandl116aa622007-08-15 14:28:22 +000034.. index:: module: glob
35
36Note that the filename separator (``'/'`` on Unix) is *not* special to this
37module. See module :mod:`glob` for pathname expansion (:mod:`glob` uses
38:func:`fnmatch` to match pathname segments). Similarly, filenames starting with
39a period are not special for this module, and are matched by the ``*`` and ``?``
40patterns.
41
42
43.. function:: fnmatch(filename, pattern)
44
Ezio Melottiec5c8b82009-09-23 21:42:25 +000045 Test whether the *filename* string matches the *pattern* string, returning
csabellae5f6e862017-06-10 00:42:11 -040046 :const:`True` or :const:`False`. Both parameters are case-normalized
47 using :func:`os.path.normcase`. :func:`fnmatchcase` can be used to perform a
Ezio Melottiec5c8b82009-09-23 21:42:25 +000048 case-sensitive comparison, regardless of whether that's standard for the
49 operating system.
Georg Brandl116aa622007-08-15 14:28:22 +000050
51 This example will print all file names in the current directory with the
52 extension ``.txt``::
53
54 import fnmatch
55 import os
56
57 for file in os.listdir('.'):
58 if fnmatch.fnmatch(file, '*.txt'):
Georg Brandl6911e3c2007-09-04 07:15:32 +000059 print(file)
Georg Brandl116aa622007-08-15 14:28:22 +000060
61
62.. function:: fnmatchcase(filename, pattern)
63
Ezio Melottiec5c8b82009-09-23 21:42:25 +000064 Test whether *filename* matches *pattern*, returning :const:`True` or
csabellae5f6e862017-06-10 00:42:11 -040065 :const:`False`; the comparison is case-sensitive and does not apply
66 :func:`os.path.normcase`.
Georg Brandl116aa622007-08-15 14:28:22 +000067
68
69.. function:: filter(names, pattern)
70
71 Return the subset of the list of *names* that match *pattern*. It is the same as
72 ``[n for n in names if fnmatch(n, pattern)]``, but implemented more efficiently.
73
Georg Brandl116aa622007-08-15 14:28:22 +000074
75.. function:: translate(pattern)
76
Serhiy Storchakaa65a4742016-10-27 22:47:08 +030077 Return the shell-style *pattern* converted to a regular expression for
78 using with :func:`re.match`.
Georg Brandl116aa622007-08-15 14:28:22 +000079
Christian Heimesfe337bf2008-03-23 21:54:12 +000080 Example:
Georg Brandl116aa622007-08-15 14:28:22 +000081
82 >>> import fnmatch, re
83 >>>
84 >>> regex = fnmatch.translate('*.txt')
85 >>> regex
Serhiy Storchakae0a220e2016-10-27 22:44:03 +030086 '(?s:.*\\.txt)\\Z'
Georg Brandl116aa622007-08-15 14:28:22 +000087 >>> reobj = re.compile(regex)
Ezio Melottiec5c8b82009-09-23 21:42:25 +000088 >>> reobj.match('foobar.txt')
Serhiy Storchaka0b5e61d2017-10-04 20:09:49 +030089 <re.Match object; span=(0, 10), match='foobar.txt'>
Georg Brandl116aa622007-08-15 14:28:22 +000090
91
Georg Brandl116aa622007-08-15 14:28:22 +000092.. seealso::
93
94 Module :mod:`glob`
95 Unix shell-style path expansion.