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 | |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 7 | **Source code:** :source:`Lib/fnmatch.py` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 8 | |
| 9 | .. index:: single: filenames; wildcard expansion |
| 10 | |
| 11 | .. index:: module: re |
| 12 | |
Raymond Hettinger | 4f707fd | 2011-01-10 19:54:11 +0000 | [diff] [blame] | 13 | -------------- |
| 14 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 15 | This module provides support for Unix shell-style wildcards, which are *not* the |
| 16 | same as regular expressions (which are documented in the :mod:`re` module). The |
| 17 | special 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 Melotti | a39a22d | 2012-11-17 17:38:11 +0200 | [diff] [blame] | 31 | For a literal match, wrap the meta-characters in brackets. |
| 32 | For example, ``'[?]'`` matches the character ``'?'``. |
| 33 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 34 | .. index:: module: glob |
| 35 | |
| 36 | Note that the filename separator (``'/'`` on Unix) is *not* special to this |
| 37 | module. See module :mod:`glob` for pathname expansion (:mod:`glob` uses |
| 38 | :func:`fnmatch` to match pathname segments). Similarly, filenames starting with |
| 39 | a period are not special for this module, and are matched by the ``*`` and ``?`` |
| 40 | patterns. |
| 41 | |
| 42 | |
| 43 | .. function:: fnmatch(filename, pattern) |
| 44 | |
Ezio Melotti | ec5c8b8 | 2009-09-23 21:42:25 +0000 | [diff] [blame] | 45 | Test whether the *filename* string matches the *pattern* string, returning |
| 46 | :const:`True` or :const:`False`. If the operating system is case-insensitive, |
| 47 | then both parameters will be normalized to all lower- or upper-case before |
| 48 | the comparison is performed. :func:`fnmatchcase` can be used to perform a |
| 49 | case-sensitive comparison, regardless of whether that's standard for the |
| 50 | operating system. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 51 | |
| 52 | This example will print all file names in the current directory with the |
| 53 | extension ``.txt``:: |
| 54 | |
| 55 | import fnmatch |
| 56 | import os |
| 57 | |
| 58 | for file in os.listdir('.'): |
| 59 | if fnmatch.fnmatch(file, '*.txt'): |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 60 | print(file) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 61 | |
| 62 | |
| 63 | .. function:: fnmatchcase(filename, pattern) |
| 64 | |
Ezio Melotti | ec5c8b8 | 2009-09-23 21:42:25 +0000 | [diff] [blame] | 65 | Test whether *filename* matches *pattern*, returning :const:`True` or |
| 66 | :const:`False`; the comparison is case-sensitive. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 67 | |
| 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 74 | |
| 75 | .. function:: translate(pattern) |
| 76 | |
Serhiy Storchaka | a65a474 | 2016-10-27 22:47:08 +0300 | [diff] [blame] | 77 | Return the shell-style *pattern* converted to a regular expression for |
| 78 | using with :func:`re.match`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 79 | |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 80 | Example: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 81 | |
| 82 | >>> import fnmatch, re |
| 83 | >>> |
| 84 | >>> regex = fnmatch.translate('*.txt') |
| 85 | >>> regex |
R David Murray | 4590c3d | 2015-05-02 15:08:22 -0400 | [diff] [blame] | 86 | '.*\\.txt\\Z(?ms)' |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 87 | >>> reobj = re.compile(regex) |
Ezio Melotti | ec5c8b8 | 2009-09-23 21:42:25 +0000 | [diff] [blame] | 88 | >>> reobj.match('foobar.txt') |
Ezio Melotti | 7571941 | 2013-11-23 20:27:27 +0200 | [diff] [blame] | 89 | <_sre.SRE_Match object; span=(0, 10), match='foobar.txt'> |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 90 | |
| 91 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 92 | .. seealso:: |
| 93 | |
| 94 | Module :mod:`glob` |
| 95 | Unix shell-style path expansion. |