~dkuhlman/python-training-materials/Materials

« back to all changes in this revision

Viewing changes to python-2.7.12-docs-html/library/fnmatch.html

  • Committer: Dave Kuhlman
  • Date: 2017-04-15 16:24:56 UTC
  • Revision ID: dkuhlman@davekuhlman.org-20170415162456-iav9vozzg4iwqwv3
Updated docs

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 
2
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
3
 
 
4
 
 
5
<html xmlns="http://www.w3.org/1999/xhtml">
 
6
  <head>
 
7
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 
8
    
 
9
    <title>10.8. fnmatch — Unix filename pattern matching &mdash; Python 2.7.12 documentation</title>
 
10
    
 
11
    <link rel="stylesheet" href="../_static/classic.css" type="text/css" />
 
12
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
 
13
    
 
14
    <script type="text/javascript">
 
15
      var DOCUMENTATION_OPTIONS = {
 
16
        URL_ROOT:    '../',
 
17
        VERSION:     '2.7.12',
 
18
        COLLAPSE_INDEX: false,
 
19
        FILE_SUFFIX: '.html',
 
20
        HAS_SOURCE:  true
 
21
      };
 
22
    </script>
 
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>
 
39
 
 
40
    
 
41
 
 
42
  </head>
 
43
  <body role="document">  
 
44
    <div class="related" role="navigation" aria-label="related navigation">
 
45
      <h3>Navigation</h3>
 
46
      <ul>
 
47
        <li class="right" style="margin-right: 10px">
 
48
          <a href="../genindex.html" title="General Index"
 
49
             accesskey="I">index</a></li>
 
50
        <li class="right" >
 
51
          <a href="../py-modindex.html" title="Python Module Index"
 
52
             >modules</a> |</li>
 
53
        <li class="right" >
 
54
          <a href="linecache.html" title="10.9. linecache — Random access to text lines"
 
55
             accesskey="N">next</a> |</li>
 
56
        <li class="right" >
 
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> &raquo;</li>
 
62
        <li>
 
63
          <span class="version_switcher_placeholder">2.7.12</span>
 
64
          <a href="../index.html">Documentation</a> &raquo;
 
65
        </li>
 
66
 
 
67
          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &raquo;</li>
 
68
          <li class="nav-item nav-item-2"><a href="filesys.html" accesskey="U">10. File and Directory Access</a> &raquo;</li> 
 
69
      </ul>
 
70
    </div>    
 
71
 
 
72
    <div class="document">
 
73
      <div class="documentwrapper">
 
74
        <div class="bodywrapper">
 
75
          <div class="body" role="main">
 
76
            
 
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> &#8212; 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">
 
85
<colgroup>
 
86
<col width="25%" />
 
87
<col width="75%" />
 
88
</colgroup>
 
89
<thead valign="bottom">
 
90
<tr class="row-odd"><th class="head">Pattern</th>
 
91
<th class="head">Meaning</th>
 
92
</tr>
 
93
</thead>
 
94
<tbody valign="top">
 
95
<tr class="row-even"><td><code class="docutils literal"><span class="pre">*</span></code></td>
 
96
<td>matches everything</td>
 
97
</tr>
 
98
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">?</span></code></td>
 
99
<td>matches any single character</td>
 
100
</tr>
 
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>
 
103
</tr>
 
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>
 
106
</tr>
 
107
</tbody>
 
108
</table>
 
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>
 
115
patterns.</p>
 
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&#8217;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>
 
129
 
 
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">&#39;.&#39;</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">&#39;*.txt&#39;</span><span class="p">):</span>
 
132
        <span class="k">print</span> <span class="nb">file</span>
 
133
</pre></div>
 
134
</div>
 
135
</dd></dl>
 
136
 
 
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>
 
142
</dd></dl>
 
143
 
 
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>
 
151
</div>
 
152
</dd></dl>
 
153
 
 
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>
 
159
<p>Example:</p>
 
160
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">fnmatch</span><span class="o">,</span> <span class="nn">re</span>
 
161
<span class="go">&gt;&gt;&gt;</span>
 
162
<span class="gp">&gt;&gt;&gt; </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">&#39;*.txt&#39;</span><span class="p">)</span>
 
163
<span class="gp">&gt;&gt;&gt; </span><span class="n">regex</span>
 
164
<span class="go">&#39;.*\\.txt\\Z(?ms)&#39;</span>
 
165
<span class="gp">&gt;&gt;&gt; </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">&gt;&gt;&gt; </span><span class="n">reobj</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s1">&#39;foobar.txt&#39;</span><span class="p">)</span>
 
167
<span class="go">&lt;_sre.SRE_Match object at 0x...&gt;</span>
 
168
</pre></div>
 
169
</div>
 
170
</dd></dl>
 
171
 
 
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>
 
177
</dl>
 
178
</div>
 
179
</div>
 
180
 
 
181
 
 
182
          </div>
 
183
        </div>
 
184
      </div>
 
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> &#8212; Unix style pathname pattern expansion</a></p>
 
190
  <h4>Next topic</h4>
 
191
  <p class="topless"><a href="linecache.html"
 
192
                        title="next chapter">10.9. <code class="docutils literal"><span class="pre">linecache</span></code> &#8212; Random access to text lines</a></p>
 
193
<h3>This Page</h3>
 
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>
 
198
</ul>
 
199
 
 
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" />
 
207
    </form>
 
208
    <p class="searchtip" style="font-size: 90%">
 
209
    Enter search terms or a module, class or function name.
 
210
    </p>
 
211
</div>
 
212
<script type="text/javascript">$('#searchbox').show(0);</script>
 
213
        </div>
 
214
      </div>
 
215
      <div class="clearer"></div>
 
216
    </div>  
 
217
    <div class="related" role="navigation" aria-label="related navigation">
 
218
      <h3>Navigation</h3>
 
219
      <ul>
 
220
        <li class="right" style="margin-right: 10px">
 
221
          <a href="../genindex.html" title="General Index"
 
222
             >index</a></li>
 
223
        <li class="right" >
 
224
          <a href="../py-modindex.html" title="Python Module Index"
 
225
             >modules</a> |</li>
 
226
        <li class="right" >
 
227
          <a href="linecache.html" title="10.9. linecache — Random access to text lines"
 
228
             >next</a> |</li>
 
229
        <li class="right" >
 
230
          <a href="glob.html" title="10.7. glob — Unix style pathname pattern expansion"
 
231
             >previous</a> |</li>
 
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> &raquo;</li>
 
235
        <li>
 
236
          <span class="version_switcher_placeholder">2.7.12</span>
 
237
          <a href="../index.html">Documentation</a> &raquo;
 
238
        </li>
 
239
 
 
240
          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &raquo;</li>
 
241
          <li class="nav-item nav-item-2"><a href="filesys.html" >10. File and Directory Access</a> &raquo;</li> 
 
242
      </ul>
 
243
    </div>  
 
244
    <div class="footer">
 
245
    &copy; <a href="../copyright.html">Copyright</a> 1990-2016, Python Software Foundation.
 
246
    <br />
 
247
    The Python Software Foundation is a non-profit corporation.
 
248
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
 
249
    <br />
 
250
    Last updated on Oct 27, 2016.
 
251
    <a href="../bugs.html">Found a bug</a>?
 
252
    <br />
 
253
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
 
254
    </div>
 
255
 
 
256
  </body>
 
257
</html>
 
 
b'\\ No newline at end of file'