1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
<html xmlns="http://www.w3.org/1999/xhtml">
7
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
9
<title>10.8. fnmatch — Unix filename pattern matching — Python 2.7.12 documentation</title>
11
<link rel="stylesheet" href="../_static/classic.css" type="text/css" />
12
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
14
<script type="text/javascript">
15
var DOCUMENTATION_OPTIONS = {
18
COLLAPSE_INDEX: false,
23
<script type="text/javascript" src="../_static/jquery.js"></script>
24
<script type="text/javascript" src="../_static/underscore.js"></script>
25
<script type="text/javascript" src="../_static/doctools.js"></script>
26
<script type="text/javascript" src="../_static/sidebar.js"></script>
27
<link rel="search" type="application/opensearchdescription+xml"
28
title="Search within Python 2.7.12 documentation"
29
href="../_static/opensearch.xml"/>
30
<link rel="author" title="About these documents" href="../about.html" />
31
<link rel="copyright" title="Copyright" href="../copyright.html" />
32
<link rel="top" title="Python 2.7.12 documentation" href="../contents.html" />
33
<link rel="up" title="10. File and Directory Access" href="filesys.html" />
34
<link rel="next" title="10.9. linecache — Random access to text lines" href="linecache.html" />
35
<link rel="prev" title="10.7. glob — Unix style pathname pattern expansion" href="glob.html" />
36
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
37
<script type="text/javascript" src="../_static/copybutton.js"></script>
38
<script type="text/javascript" src="../_static/version_switch.js"></script>
43
<body role="document">
44
<div class="related" role="navigation" aria-label="related navigation">
47
<li class="right" style="margin-right: 10px">
48
<a href="../genindex.html" title="General Index"
49
accesskey="I">index</a></li>
51
<a href="../py-modindex.html" title="Python Module Index"
54
<a href="linecache.html" title="10.9. linecache — Random access to text lines"
55
accesskey="N">next</a> |</li>
57
<a href="glob.html" title="10.7. glob — Unix style pathname pattern expansion"
58
accesskey="P">previous</a> |</li>
59
<li><img src="../_static/py.png" alt=""
60
style="vertical-align: middle; margin-top: -1px"/></li>
61
<li><a href="https://www.python.org/">Python</a> »</li>
63
<span class="version_switcher_placeholder">2.7.12</span>
64
<a href="../index.html">Documentation</a> »
67
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
68
<li class="nav-item nav-item-2"><a href="filesys.html" accesskey="U">10. File and Directory Access</a> »</li>
72
<div class="document">
73
<div class="documentwrapper">
74
<div class="bodywrapper">
75
<div class="body" role="main">
77
<div class="section" id="module-fnmatch">
78
<span id="fnmatch-unix-filename-pattern-matching"></span><h1>10.8. <a class="reference internal" href="#module-fnmatch" title="fnmatch: Unix shell style filename pattern matching."><code class="xref py py-mod docutils literal"><span class="pre">fnmatch</span></code></a> — Unix filename pattern matching<a class="headerlink" href="#module-fnmatch" title="Permalink to this headline">¶</a></h1>
79
<span class="target" id="index-0"></span><p id="index-1"><strong>Source code:</strong> <a class="reference external" href="https://hg.python.org/cpython/file/2.7/Lib/fnmatch.py">Lib/fnmatch.py</a></p>
80
<hr class="docutils" />
81
<p>This module provides support for Unix shell-style wildcards, which are <em>not</em> the
82
same as regular expressions (which are documented in the <a class="reference internal" href="re.html#module-re" title="re: Regular expression operations."><code class="xref py py-mod docutils literal"><span class="pre">re</span></code></a> module). The
83
special characters used in shell-style wildcards are:</p>
84
<table border="1" class="docutils">
89
<thead valign="bottom">
90
<tr class="row-odd"><th class="head">Pattern</th>
91
<th class="head">Meaning</th>
95
<tr class="row-even"><td><code class="docutils literal"><span class="pre">*</span></code></td>
96
<td>matches everything</td>
98
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">?</span></code></td>
99
<td>matches any single character</td>
101
<tr class="row-even"><td><code class="docutils literal"><span class="pre">[seq]</span></code></td>
102
<td>matches any character in <em>seq</em></td>
104
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">[!seq]</span></code></td>
105
<td>matches any character not in <em>seq</em></td>
109
<p>For a literal match, wrap the meta-characters in brackets.
110
For example, <code class="docutils literal"><span class="pre">'[?]'</span></code> matches the character <code class="docutils literal"><span class="pre">'?'</span></code>.</p>
111
<p id="index-2">Note that the filename separator (<code class="docutils literal"><span class="pre">'/'</span></code> on Unix) is <em>not</em> special to this
112
module. See module <a class="reference internal" href="glob.html#module-glob" title="glob: Unix shell style pathname pattern expansion."><code class="xref py py-mod docutils literal"><span class="pre">glob</span></code></a> for pathname expansion (<a class="reference internal" href="glob.html#module-glob" title="glob: Unix shell style pathname pattern expansion."><code class="xref py py-mod docutils literal"><span class="pre">glob</span></code></a> uses
113
<a class="reference internal" href="#module-fnmatch" title="fnmatch: Unix shell style filename pattern matching."><code class="xref py py-func docutils literal"><span class="pre">fnmatch()</span></code></a> to match pathname segments). Similarly, filenames starting with
114
a period are not special for this module, and are matched by the <code class="docutils literal"><span class="pre">*</span></code> and <code class="docutils literal"><span class="pre">?</span></code>
116
<dl class="function">
117
<dt id="fnmatch.fnmatch">
118
<code class="descclassname">fnmatch.</code><code class="descname">fnmatch</code><span class="sig-paren">(</span><em>filename</em>, <em>pattern</em><span class="sig-paren">)</span><a class="headerlink" href="#fnmatch.fnmatch" title="Permalink to this definition">¶</a></dt>
119
<dd><p>Test whether the <em>filename</em> string matches the <em>pattern</em> string, returning
120
<a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal"><span class="pre">True</span></code></a> or <a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal"><span class="pre">False</span></code></a>. If the operating system is case-insensitive,
121
then both parameters will be normalized to all lower- or upper-case before
122
the comparison is performed. <a class="reference internal" href="#fnmatch.fnmatchcase" title="fnmatch.fnmatchcase"><code class="xref py py-func docutils literal"><span class="pre">fnmatchcase()</span></code></a> can be used to perform a
123
case-sensitive comparison, regardless of whether that’s standard for the
124
operating system.</p>
125
<p>This example will print all file names in the current directory with the
126
extension <code class="docutils literal"><span class="pre">.txt</span></code>:</p>
127
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">fnmatch</span>
128
<span class="kn">import</span> <span class="nn">os</span>
130
<span class="k">for</span> <span class="nb">file</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="s1">'.'</span><span class="p">):</span>
131
<span class="k">if</span> <span class="n">fnmatch</span><span class="o">.</span><span class="n">fnmatch</span><span class="p">(</span><span class="nb">file</span><span class="p">,</span> <span class="s1">'*.txt'</span><span class="p">):</span>
132
<span class="k">print</span> <span class="nb">file</span>
137
<dl class="function">
138
<dt id="fnmatch.fnmatchcase">
139
<code class="descclassname">fnmatch.</code><code class="descname">fnmatchcase</code><span class="sig-paren">(</span><em>filename</em>, <em>pattern</em><span class="sig-paren">)</span><a class="headerlink" href="#fnmatch.fnmatchcase" title="Permalink to this definition">¶</a></dt>
140
<dd><p>Test whether <em>filename</em> matches <em>pattern</em>, returning <a class="reference internal" href="constants.html#True" title="True"><code class="xref py py-const docutils literal"><span class="pre">True</span></code></a> or
141
<a class="reference internal" href="constants.html#False" title="False"><code class="xref py py-const docutils literal"><span class="pre">False</span></code></a>; the comparison is case-sensitive.</p>
144
<dl class="function">
145
<dt id="fnmatch.filter">
146
<code class="descclassname">fnmatch.</code><code class="descname">filter</code><span class="sig-paren">(</span><em>names</em>, <em>pattern</em><span class="sig-paren">)</span><a class="headerlink" href="#fnmatch.filter" title="Permalink to this definition">¶</a></dt>
147
<dd><p>Return the subset of the list of <em>names</em> that match <em>pattern</em>. It is the same as
148
<code class="docutils literal"><span class="pre">[n</span> <span class="pre">for</span> <span class="pre">n</span> <span class="pre">in</span> <span class="pre">names</span> <span class="pre">if</span> <span class="pre">fnmatch(n,</span> <span class="pre">pattern)]</span></code>, but implemented more efficiently.</p>
149
<div class="versionadded">
150
<p><span class="versionmodified">New in version 2.2.</span></p>
154
<dl class="function">
155
<dt id="fnmatch.translate">
156
<code class="descclassname">fnmatch.</code><code class="descname">translate</code><span class="sig-paren">(</span><em>pattern</em><span class="sig-paren">)</span><a class="headerlink" href="#fnmatch.translate" title="Permalink to this definition">¶</a></dt>
157
<dd><p>Return the shell-style <em>pattern</em> converted to a regular expression for
158
using with <a class="reference internal" href="re.html#re.match" title="re.match"><code class="xref py py-func docutils literal"><span class="pre">re.match()</span></code></a>.</p>
160
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">fnmatch</span><span class="o">,</span> <span class="nn">re</span>
161
<span class="go">>>></span>
162
<span class="gp">>>> </span><span class="n">regex</span> <span class="o">=</span> <span class="n">fnmatch</span><span class="o">.</span><span class="n">translate</span><span class="p">(</span><span class="s1">'*.txt'</span><span class="p">)</span>
163
<span class="gp">>>> </span><span class="n">regex</span>
164
<span class="go">'.*\\.txt\\Z(?ms)'</span>
165
<span class="gp">>>> </span><span class="n">reobj</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="n">regex</span><span class="p">)</span>
166
<span class="gp">>>> </span><span class="n">reobj</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s1">'foobar.txt'</span><span class="p">)</span>
167
<span class="go"><_sre.SRE_Match object at 0x...></span>
172
<div class="admonition seealso">
173
<p class="first admonition-title">See also</p>
174
<dl class="last docutils">
175
<dt>Module <a class="reference internal" href="glob.html#module-glob" title="glob: Unix shell style pathname pattern expansion."><code class="xref py py-mod docutils literal"><span class="pre">glob</span></code></a></dt>
176
<dd>Unix shell-style path expansion.</dd>
185
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
186
<div class="sphinxsidebarwrapper">
187
<h4>Previous topic</h4>
188
<p class="topless"><a href="glob.html"
189
title="previous chapter">10.7. <code class="docutils literal"><span class="pre">glob</span></code> — Unix style pathname pattern expansion</a></p>
191
<p class="topless"><a href="linecache.html"
192
title="next chapter">10.9. <code class="docutils literal"><span class="pre">linecache</span></code> — Random access to text lines</a></p>
194
<ul class="this-page-menu">
195
<li><a href="../bugs.html">Report a Bug</a></li>
196
<li><a href="../_sources/library/fnmatch.txt"
197
rel="nofollow">Show Source</a></li>
200
<div id="searchbox" style="display: none" role="search">
201
<h3>Quick search</h3>
202
<form class="search" action="../search.html" method="get">
203
<input type="text" name="q" />
204
<input type="submit" value="Go" />
205
<input type="hidden" name="check_keywords" value="yes" />
206
<input type="hidden" name="area" value="default" />
208
<p class="searchtip" style="font-size: 90%">
209
Enter search terms or a module, class or function name.
212
<script type="text/javascript">$('#searchbox').show(0);</script>
215
<div class="clearer"></div>
217
<div class="related" role="navigation" aria-label="related navigation">
220
<li class="right" style="margin-right: 10px">
221
<a href="../genindex.html" title="General Index"
224
<a href="../py-modindex.html" title="Python Module Index"
227
<a href="linecache.html" title="10.9. linecache — Random access to text lines"
230
<a href="glob.html" title="10.7. glob — Unix style pathname pattern expansion"
232
<li><img src="../_static/py.png" alt=""
233
style="vertical-align: middle; margin-top: -1px"/></li>
234
<li><a href="https://www.python.org/">Python</a> »</li>
236
<span class="version_switcher_placeholder">2.7.12</span>
237
<a href="../index.html">Documentation</a> »
240
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
241
<li class="nav-item nav-item-2"><a href="filesys.html" >10. File and Directory Access</a> »</li>
245
© <a href="../copyright.html">Copyright</a> 1990-2016, Python Software Foundation.
247
The Python Software Foundation is a non-profit corporation.
248
<a href="https://www.python.org/psf/donations/">Please donate.</a>
250
Last updated on Oct 27, 2016.
251
<a href="../bugs.html">Found a bug</a>?
253
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
b'\\ No newline at end of file'