]> granicus.if.org Git - python/commitdiff
Patch #1679379: add documentation for fnmatch.translate().
authorGeorg Brandl <georg@python.org>
Tue, 13 Mar 2007 07:51:04 +0000 (07:51 +0000)
committerGeorg Brandl <georg@python.org>
Tue, 13 Mar 2007 07:51:04 +0000 (07:51 +0000)
 (backport from rev. 54323)

Doc/lib/libfnmatch.tex
Misc/NEWS

index fc4b97a3126ab9488c27b819f9f12b4875094b45..1ac46bdd4214354ce0ba43ca9a98c5a5a8e236a7 100644 (file)
@@ -36,6 +36,19 @@ lower- or upper-case before the comparison is performed.  If you
 require a case-sensitive comparison regardless of whether that's
 standard for your operating system, use \function{fnmatchcase()}
 instead.
+
+This example will print all file names in the current directory with the
+extension \code{.txt}:
+
+\begin{verbatim}
+import fnmatch
+import os
+
+for file in os.listdir('.'):
+    if fnmatch.fnmatch(file, '*.txt'):
+        print file
+\end{verbatim}
+
 \end{funcdesc}
 
 \begin{funcdesc}{fnmatchcase}{filename, pattern}
@@ -50,6 +63,24 @@ implemented more efficiently.
 \versionadded{2.2}
 \end{funcdesc}
 
+\begin{funcdesc}{translate}{pattern}
+Return the shell-style \var{pattern} converted to a regular
+expression.
+
+Example:
+
+\begin{verbatim}
+>>> import fnmatch, re
+>>>
+>>> regex = fnmatch.translate('*.txt')
+>>> regex
+'.*\\.txt$'
+>>> reobj = re.compile(regex)
+>>> print reobj.match('foobar.txt')
+<_sre.SRE_Match object at 0x...>
+\end{verbatim}
+\end{funcdesc}
+
 \begin{seealso}
   \seemodule{glob}{\UNIX{} shell-style path expansion.}
 \end{seealso}
index c21972d37a71dbb70bae0da55704579e6dd28ef7..966b9a7133395b826e11fcf35a7cd62f6cec1a17 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -503,6 +503,8 @@ Tests
 Documentation
 -------------
 
+- Patch #1679379: add documentation for fnmatch.translate().
+
 - Patch #1671450: add a section about subclassing builtin types to the
   "extending and embedding" tutorial.