10.8. "fnmatch" — Filtrage par motif des noms de fichiers Unix
**************************************************************

**Code source :** Lib/fnmatch.py

======================================================================

Ce module fournit la gestion des caractères de remplacement de style
shell Unix, qui ne sont *pas* identiques à ceux utilisés dans les
expressions régulières (documentés dans le module "re"). Les
caractères spéciaux utilisés comme caractères de remplacement de style
shell sont :

+--------------+--------------------------------------+
| Motif        | Signification                        |
+==============+======================================+
| "*"          | reconnaît n’importe quoi             |
+--------------+--------------------------------------+
| "?"          | reconnaît n’importe quel caractère   |
|              | unique                               |
+--------------+--------------------------------------+
| "[seq]"      | reconnaît n’importe quel caractère   |
|              | dans *seq*                           |
+--------------+--------------------------------------+
| "[!seq]"     | reconnaît n’importe quel caractère   |
|              | qui n’est pas dans *seq*             |
+--------------+--------------------------------------+

Pour une correspondance littérale, il faut entourer le métacaractère
par des crochets. Par exemple, "'[?]'" reconnaît le caractère "'?'".

Note that the filename separator ("'/'" on Unix) is *not* special to
this module.  See module "glob" for pathname expansion ("glob" uses
"filter()" to match pathname segments).  Similarly, filenames starting
with a period are not special for this module, and are matched by the
"*" and "?" patterns.

fnmatch.fnmatch(filename, pattern)

   Test whether the *filename* string matches the *pattern* string,
   returning "True" or "False".  Both parameters are case-normalized
   using "os.path.normcase()". "fnmatchcase()" can be used to perform
   a case-sensitive comparison, regardless of whether that’s standard
   for the operating system.

   Cet exemple affiche tous les noms de fichiers du répertoire courant
   ayant pour extension ".txt" :

      import fnmatch
      import os

      for file in os.listdir('.'):
          if fnmatch.fnmatch(file, '*.txt'):
              print file

fnmatch.fnmatchcase(filename, pattern)

   Test whether *filename* matches *pattern*, returning "True" or
   "False"; the comparison is case-sensitive and does not apply
   "os.path.normcase()".

fnmatch.filter(names, pattern)

   Renvoie un sous-ensemble de la liste *names* correspondant au motif
   *pattern*. Similaire à "[n for n in names if fnmatch(n, pattern)]",
   mais implémenté plus efficacement.

   Nouveau dans la version 2.2.

fnmatch.translate(pattern)

   Return the shell-style *pattern* converted to a regular expression
   for using with "re.match()".

   Exemple :

   >>> import fnmatch, re
   >>>
   >>> regex = fnmatch.translate('*.txt')
   >>> regex
   '.*\\.txt\\Z(?ms)'
   >>> reobj = re.compile(regex)
   >>> reobj.match('foobar.txt')
   <_sre.SRE_Match object at 0x...>

Voir aussi:

  Module "glob"
     Recherche de chemins de style shell Unix
