1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
<span id="fnmatch-unix-filename-pattern-matching"></span><h1>fnmatch — Unix filename pattern matching</h1> <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/fnmatch.py">Lib/fnmatch.py</a></p> <span class="target" id="index-0"></span> <p>This module provides support for Unix shell-style wildcards, which are <em>not</em> the same as regular expressions (which are documented in the <a class="reference internal" href="re#module-re" title="re: Regular expression operations."><code>re</code></a> module). The special characters used in shell-style wildcards are:</p> <table class="docutils align-default" id="index-2"> <thead> <tr>
<th class="head"><p>Pattern</p></th> <th class="head"><p>Meaning</p></th> </tr> </thead> <tr>
<td><p><code>*</code></p></td> <td><p>matches everything</p></td> </tr> <tr>
<td><p><code>?</code></p></td> <td><p>matches any single character</p></td> </tr> <tr>
<td><p><code>[seq]</code></p></td> <td><p>matches any character in <em>seq</em></p></td> </tr> <tr>
<td><p><code>[!seq]</code></p></td> <td><p>matches any character not in <em>seq</em></p></td> </tr> </table> <p>For a literal match, wrap the meta-characters in brackets. For example, <code>'[?]'</code> matches the character <code>'?'</code>.</p> <p id="index-3">Note that the filename separator (<code>'/'</code> on Unix) is <em>not</em> special to this module. See module <a class="reference internal" href="glob#module-glob" title="glob: Unix shell style pathname pattern expansion."><code>glob</code></a> for pathname expansion (<a class="reference internal" href="glob#module-glob" title="glob: Unix shell style pathname pattern expansion."><code>glob</code></a> uses <a class="reference internal" href="#fnmatch.filter" title="fnmatch.filter"><code>filter()</code></a> to match pathname segments). Similarly, filenames starting with a period are not special for this module, and are matched by the <code>*</code> and <code>?</code> patterns.</p> <p>Also note that <a class="reference internal" href="functools#functools.lru_cache" title="functools.lru_cache"><code>functools.lru_cache()</code></a> with the <em>maxsize</em> of 32768 is used to cache the compiled regex patterns in the following functions: <a class="reference internal" href="#module-fnmatch" title="fnmatch: Unix shell style filename pattern matching."><code>fnmatch()</code></a>, <a class="reference internal" href="#fnmatch.fnmatchcase" title="fnmatch.fnmatchcase"><code>fnmatchcase()</code></a>, <a class="reference internal" href="#fnmatch.filter" title="fnmatch.filter"><code>filter()</code></a>.</p> <dl class="py function"> <dt class="sig sig-object py" id="fnmatch.fnmatch">
<code>fnmatch.fnmatch(filename, pattern)</code> </dt> <dd>
<p>Test whether the <em>filename</em> string matches the <em>pattern</em> string, returning <a class="reference internal" href="constants#True" title="True"><code>True</code></a> or <a class="reference internal" href="constants#False" title="False"><code>False</code></a>. Both parameters are case-normalized using <a class="reference internal" href="os.path#os.path.normcase" title="os.path.normcase"><code>os.path.normcase()</code></a>. <a class="reference internal" href="#fnmatch.fnmatchcase" title="fnmatch.fnmatchcase"><code>fnmatchcase()</code></a> can be used to perform a case-sensitive comparison, regardless of whether that’s standard for the operating system.</p> <p>This example will print all file names in the current directory with the extension <code>.txt</code>:</p> <pre data-language="python">import fnmatch
import os
for file in os.listdir('.'):
if fnmatch.fnmatch(file, '*.txt'):
print(file)
</pre> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="fnmatch.fnmatchcase">
<code>fnmatch.fnmatchcase(filename, pattern)</code> </dt> <dd>
<p>Test whether <em>filename</em> matches <em>pattern</em>, returning <a class="reference internal" href="constants#True" title="True"><code>True</code></a> or <a class="reference internal" href="constants#False" title="False"><code>False</code></a>; the comparison is case-sensitive and does not apply <a class="reference internal" href="os.path#os.path.normcase" title="os.path.normcase"><code>os.path.normcase()</code></a>.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="fnmatch.filter">
<code>fnmatch.filter(names, pattern)</code> </dt> <dd>
<p>Construct a list from those elements of the iterable <em>names</em> that match <em>pattern</em>. It is the same as <code>[n for n in names if fnmatch(n, pattern)]</code>, but implemented more efficiently.</p> </dd>
</dl> <dl class="py function"> <dt class="sig sig-object py" id="fnmatch.translate">
<code>fnmatch.translate(pattern)</code> </dt> <dd>
<p>Return the shell-style <em>pattern</em> converted to a regular expression for using with <a class="reference internal" href="re#re.match" title="re.match"><code>re.match()</code></a>.</p> <p>Example:</p> <pre data-language="python">>>> import fnmatch, re
>>>
>>> regex = fnmatch.translate('*.txt')
>>> regex
'(?s:.*\\.txt)\\Z'
>>> reobj = re.compile(regex)
>>> reobj.match('foobar.txt')
<re.Match object; span=(0, 10), match='foobar.txt'>
</pre> </dd>
</dl> <div class="admonition seealso"> <p class="admonition-title">See also</p> <dl class="simple"> <dt>
<code>Module</code> <a class="reference internal" href="glob#module-glob" title="glob: Unix shell style pathname pattern expansion."><code>glob</code></a>
</dt>
<dd>
<p>Unix shell-style path expansion.</p> </dd> </dl> </div> <div class="_attribution">
<p class="_attribution-p">
© 2001–2023 Python Software Foundation<br>Licensed under the PSF License.<br>
<a href="https://docs.python.org/3.12/library/fnmatch.html" class="_attribution-link">https://docs.python.org/3.12/library/fnmatch.html</a>
</p>
</div>
|