Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`fnmatch` --- Unix filename pattern matching |
| 2 | ================================================= |
| 3 | |
| 4 | .. module:: fnmatch |
| 5 | :synopsis: Unix shell style filename pattern matching. |
| 6 | |
| 7 | |
| 8 | .. index:: single: filenames; wildcard expansion |
| 9 | |
| 10 | .. index:: module: re |
| 11 | |
| 12 | This module provides support for Unix shell-style wildcards, which are *not* the |
| 13 | same as regular expressions (which are documented in the :mod:`re` module). The |
| 14 | special characters used in shell-style wildcards are: |
| 15 | |
| 16 | +------------+------------------------------------+ |
| 17 | | Pattern | Meaning | |
| 18 | +============+====================================+ |
| 19 | | ``*`` | matches everything | |
| 20 | +------------+------------------------------------+ |
| 21 | | ``?`` | matches any single character | |
| 22 | +------------+------------------------------------+ |
| 23 | | ``[seq]`` | matches any character in *seq* | |
| 24 | +------------+------------------------------------+ |
| 25 | | ``[!seq]`` | matches any character not in *seq* | |
| 26 | +------------+------------------------------------+ |
| 27 | |
| 28 | .. index:: module: glob |
| 29 | |
| 30 | Note that the filename separator (``'/'`` on Unix) is *not* special to this |
| 31 | module. See module :mod:`glob` for pathname expansion (:mod:`glob` uses |
| 32 | :func:`fnmatch` to match pathname segments). Similarly, filenames starting with |
| 33 | a period are not special for this module, and are matched by the ``*`` and ``?`` |
| 34 | patterns. |
| 35 | |
| 36 | |
| 37 | .. function:: fnmatch(filename, pattern) |
| 38 | |
| 39 | Test whether the *filename* string matches the *pattern* string, returning true |
| 40 | or false. If the operating system is case-insensitive, then both parameters |
| 41 | will be normalized to all lower- or upper-case before the comparison is |
| 42 | performed. If you require a case-sensitive comparison regardless of whether |
| 43 | that's standard for your operating system, use :func:`fnmatchcase` instead. |
| 44 | |
| 45 | This example will print all file names in the current directory with the |
| 46 | extension ``.txt``:: |
| 47 | |
| 48 | import fnmatch |
| 49 | import os |
| 50 | |
| 51 | for file in os.listdir('.'): |
| 52 | if fnmatch.fnmatch(file, '*.txt'): |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 53 | print(file) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 54 | |
| 55 | |
| 56 | .. function:: fnmatchcase(filename, pattern) |
| 57 | |
| 58 | Test whether *filename* matches *pattern*, returning true or false; the |
| 59 | comparison is case-sensitive. |
| 60 | |
| 61 | |
| 62 | .. function:: filter(names, pattern) |
| 63 | |
| 64 | Return the subset of the list of *names* that match *pattern*. It is the same as |
| 65 | ``[n for n in names if fnmatch(n, pattern)]``, but implemented more efficiently. |
| 66 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 67 | |
| 68 | .. function:: translate(pattern) |
| 69 | |
| 70 | Return the shell-style *pattern* converted to a regular expression. |
| 71 | |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 72 | Example: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 73 | |
| 74 | >>> import fnmatch, re |
| 75 | >>> |
| 76 | >>> regex = fnmatch.translate('*.txt') |
| 77 | >>> regex |
| 78 | '.*\\.txt$' |
| 79 | >>> reobj = re.compile(regex) |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 80 | >>> print(reobj.match('foobar.txt')) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 81 | <_sre.SRE_Match object at 0x...> |
| 82 | |
| 83 | |
| 84 | .. seealso:: |
| 85 | |
| 86 | Module :mod:`glob` |
| 87 | Unix shell-style path expansion. |
| 88 | |