2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
:mod:`fnmatch` --- Unix filename pattern matching
|
|
|
|
=================================================
|
|
|
|
|
|
|
|
.. module:: fnmatch
|
|
|
|
:synopsis: Unix shell style filename pattern matching.
|
|
|
|
|
|
|
|
|
|
|
|
.. index:: single: filenames; wildcard expansion
|
|
|
|
|
|
|
|
.. index:: module: re
|
|
|
|
|
|
|
|
This module provides support for Unix shell-style wildcards, which are *not* the
|
|
|
|
same as regular expressions (which are documented in the :mod:`re` module). The
|
|
|
|
special characters used in shell-style wildcards are:
|
|
|
|
|
|
|
|
+------------+------------------------------------+
|
|
|
|
| Pattern | Meaning |
|
|
|
|
+============+====================================+
|
|
|
|
| ``*`` | matches everything |
|
|
|
|
+------------+------------------------------------+
|
|
|
|
| ``?`` | matches any single character |
|
|
|
|
+------------+------------------------------------+
|
|
|
|
| ``[seq]`` | matches any character in *seq* |
|
|
|
|
+------------+------------------------------------+
|
|
|
|
| ``[!seq]`` | matches any character not in *seq* |
|
|
|
|
+------------+------------------------------------+
|
|
|
|
|
|
|
|
.. index:: module: glob
|
|
|
|
|
|
|
|
Note that the filename separator (``'/'`` on Unix) is *not* special to this
|
|
|
|
module. See module :mod:`glob` for pathname expansion (:mod:`glob` uses
|
|
|
|
:func:`fnmatch` to match pathname segments). Similarly, filenames starting with
|
|
|
|
a period are not special for this module, and are matched by the ``*`` and ``?``
|
|
|
|
patterns.
|
|
|
|
|
2010-11-06 04:10:31 -03:00
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
Latest version of the `fnmatch Python source code
|
|
|
|
<http://svn.python.org/view/python/branches/release27-maint/Lib/fnmatch.py?view=markup>`_
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
.. function:: fnmatch(filename, pattern)
|
|
|
|
|
2009-09-23 18:36:39 -03:00
|
|
|
Test whether the *filename* string matches the *pattern* string, returning
|
|
|
|
:const:`True` or :const:`False`. If the operating system is case-insensitive,
|
|
|
|
then both parameters will be normalized to all lower- or upper-case before
|
|
|
|
the comparison is performed. :func:`fnmatchcase` can be used to perform a
|
|
|
|
case-sensitive comparison, regardless of whether that's standard for the
|
|
|
|
operating system.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
This example will print all file names in the current directory with the
|
|
|
|
extension ``.txt``::
|
|
|
|
|
|
|
|
import fnmatch
|
|
|
|
import os
|
|
|
|
|
|
|
|
for file in os.listdir('.'):
|
|
|
|
if fnmatch.fnmatch(file, '*.txt'):
|
|
|
|
print file
|
|
|
|
|
|
|
|
|
|
|
|
.. function:: fnmatchcase(filename, pattern)
|
|
|
|
|
2009-09-23 18:36:39 -03:00
|
|
|
Test whether *filename* matches *pattern*, returning :const:`True` or
|
|
|
|
:const:`False`; the comparison is case-sensitive.
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. function:: filter(names, pattern)
|
|
|
|
|
|
|
|
Return the subset of the list of *names* that match *pattern*. It is the same as
|
|
|
|
``[n for n in names if fnmatch(n, pattern)]``, but implemented more efficiently.
|
|
|
|
|
|
|
|
.. versionadded:: 2.2
|
|
|
|
|
|
|
|
|
|
|
|
.. function:: translate(pattern)
|
|
|
|
|
|
|
|
Return the shell-style *pattern* converted to a regular expression.
|
|
|
|
|
Merged revisions 82879,82958-82959,83109,83127,83224,83747,84162 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
........
r82879 | stefan.krah | 2010-07-14 12:16:11 +0200 (Mi, 14 Jul 2010) | 3 lines
High byte is the exit status.
........
r82958 | jean-paul.calderone | 2010-07-18 18:13:27 +0200 (So, 18 Jul 2010) | 1 line
There is no method named "register(fd, eventmask)"; fix markup to just indicate this is code.
........
r82959 | jean-paul.calderone | 2010-07-18 18:30:31 +0200 (So, 18 Jul 2010) | 1 line
Document the extra epoll flags
........
r83109 | brett.cannon | 2010-07-23 18:58:21 +0200 (Fr, 23 Jul 2010) | 4 lines
Mention in the fnmatch docs that meta-characters in translate cannot be quoted.
Closes issue 9358. Thanks to Brian Brazil for the patch.
........
r83127 | martin.v.loewis | 2010-07-24 12:09:11 +0200 (Sa, 24 Jul 2010) | 2 lines
Put listdir default argument into declaration.
........
r83224 | mark.dickinson | 2010-07-29 15:56:56 +0200 (Do, 29 Jul 2010) | 1 line
Fix typo.
........
r83747 | gerhard.haering | 2010-08-06 08:12:05 +0200 (Fr, 06 Aug 2010) | 2 lines
Issue #3854: Documented using the sqlite3 module with multiple threads.
........
r84162 | victor.stinner | 2010-08-18 00:01:02 +0200 (Mi, 18 Aug 2010) | 3 lines
Replace :func: by :cfunc:
To get links to the C functions
........
2010-10-06 06:47:17 -03:00
|
|
|
Be aware there is no way to quote meta-characters.
|
|
|
|
|
2008-03-22 19:04:10 -03:00
|
|
|
Example:
|
2007-08-15 11:28:01 -03:00
|
|
|
|
|
|
|
>>> import fnmatch, re
|
|
|
|
>>>
|
|
|
|
>>> regex = fnmatch.translate('*.txt')
|
|
|
|
>>> regex
|
|
|
|
'.*\\.txt$'
|
|
|
|
>>> reobj = re.compile(regex)
|
2009-09-23 18:36:39 -03:00
|
|
|
>>> reobj.match('foobar.txt')
|
2007-08-15 11:28:01 -03:00
|
|
|
<_sre.SRE_Match object at 0x...>
|
|
|
|
|
|
|
|
|
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
Module :mod:`glob`
|
|
|
|
Unix shell-style path expansion.
|
|
|
|
|