uTools-Manuals/docs/python/fnmatch.html
2019-04-21 11:50:48 +08:00

15 lines
9.3 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<div class="body" role="main"><div class="section" id="module-fnmatch"><h1><span class="yiyi-st" id="yiyi-10">11.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文件名模式的匹配</span></h1><p><span class="yiyi-st" id="yiyi-11"><strong>源代码:</strong> <a class="reference external" href="https://hg.python.org/cpython/file/3.5/Lib/fnmatch.py">Lib/fnmatch.py</a></span></p><p><span class="yiyi-st" id="yiyi-12">此模块支持Unix shell样式通配符<em>不是</em>与正则表达式(在<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>模块中记录)相同。</span><span class="yiyi-st" id="yiyi-13">shell样式通配符中使用的特殊字符是</span></p><table border="1" class="docutils"><thead valign="bottom"><tr class="row-odd"><th class="head"><span class="yiyi-st" id="yiyi-14">模式</span></th><th class="head"><span class="yiyi-st" id="yiyi-15">含义</span></th></tr></thead><tbody valign="top"><tr class="row-even"><td><span class="yiyi-st" id="yiyi-16"><code class="docutils literal"><span class="pre">*</span></code></span></td><td><span class="yiyi-st" id="yiyi-17">匹配一切</span></td></tr><tr class="row-odd"><td><span class="yiyi-st" id="yiyi-18"><code class="docutils literal"><span class="pre">?</span></code></span></td><td><span class="yiyi-st" id="yiyi-19">匹配任何单个字符</span></td></tr><tr class="row-even"><td><span class="yiyi-st" id="yiyi-20"><code class="docutils literal"><span class="pre">[seq]</span></code></span></td><td><span class="yiyi-st" id="yiyi-21">匹配<em>seq</em>中的任何字符</span></td></tr><tr class="row-odd"><td><span class="yiyi-st" id="yiyi-22"><code class="docutils literal"><span class="pre">[!seq]</span></code></span></td><td><span class="yiyi-st" id="yiyi-23">匹配不在<em>seq</em>中的任何字符</span></td></tr></tbody></table><p><span class="yiyi-st" id="yiyi-24">对于字面值匹配,将元字符包裹在括号中。</span><span class="yiyi-st" id="yiyi-25">例如,<code class="docutils literal"><span class="pre">'[?]'</span></code></span><span class="yiyi-st" id="yiyi-26">匹配字符<code class="docutils literal"><span class="pre">'?'</span></code></span></p><p id="index-2"><span class="yiyi-st" id="yiyi-27">请注意文件名分隔符Unix上的<code class="docutils literal"><span class="pre">'/'</span></code>)是<em>而不是</em>特殊于此模块。</span><span class="yiyi-st" id="yiyi-28">有关路径名扩展(<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>使用<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>匹配路径名段),请参见模块<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></span><span class="yiyi-st" id="yiyi-29">类似地,以句点开头的文件名对于此模块不是特殊的,并且由<code class="docutils literal"><span class="pre">*</span></code><code class="docutils literal"><span class="pre">?</span></code></span><span class="yiyi-st" id="yiyi-30">模式。</span></p><dl class="function"><dt id="fnmatch.fnmatch"><span class="yiyi-st" id="yiyi-31"> <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></span></dt><dd><p><span class="yiyi-st" id="yiyi-32">测试<em>filename</em>字符串是否匹配<em>模式</em>字符串,返回<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><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></span><span class="yiyi-st" id="yiyi-33">如果操作系统不区分大小写,则在执行比较之前,两个参数都将归一化为所有大小写。</span><span class="yiyi-st" id="yiyi-34"><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>可用于执行区分大小写的比较,无论是否为操作系统的标准。</span></p><p><span class="yiyi-st" id="yiyi-35">此示例将使用扩展名<code class="docutils literal"><span class="pre">.txt</span></code>打印当前目录中的所有文件名:</span></p><pre><code class="language-python"><span></span><span class="kn">import</span> <span class="nn">fnmatch</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="k">for</span> <span class="n">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>
<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="n">file</span><span class="p">,</span> <span class="s1">'*.txt'</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="n">file</span><span class="p">)</span>
</code></pre></dd></dl><dl class="function"><dt id="fnmatch.fnmatchcase"><span class="yiyi-st" id="yiyi-36"> <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></span></dt><dd><p><span class="yiyi-st" id="yiyi-37">测试<em>filename</em>是否匹配<em>模式</em>,返回<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><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>;比较是区分大小写的。</span></p></dd></dl><dl class="function"><dt id="fnmatch.filter"><span class="yiyi-st" id="yiyi-38"> <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></span></dt><dd><p><span class="yiyi-st" id="yiyi-39">返回与<em>模式</em>匹配的<em>名称</em>列表的子集。</span><span class="yiyi-st" id="yiyi-40">它与<span class="pre"><span class="pre">名称中的<code class="docutils literal"><span class="pre">[n</span> <span class="pre"></span> <span class="pre">n</span> <span class="pre"></span></code></span> t6&gt; if <span class="pre">fnmatchn</span> <span class="pre">pattern]</span>,但实现更有效。</span></span></p></dd></dl><dl class="function"><dt id="fnmatch.translate"><span class="yiyi-st" id="yiyi-41"> <code class="descclassname">fnmatch.</code><code class="descname">translate</code><span class="sig-paren">(</span><em>pattern</em><span class="sig-paren">)</span></span></dt><dd><p><span class="yiyi-st" id="yiyi-42">返回转换为正则表达式的shell样式<em>模式</em></span></p><p><span class="yiyi-st" id="yiyi-43">例:</span></p><pre><code class="language-python"><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>
<span class="go">&gt;&gt;&gt;</span>
<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">'*.txt'</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">regex</span>
<span class="go">'.*\\.txt\\Z(?ms)'</span>
<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>
<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">'foobar.txt'</span><span class="p">)</span>
<span class="go">&lt;_sre.SRE_Match object; span=(0, 10), match='foobar.txt'&gt;</span>
</code></pre></dd></dl><div class="admonition seealso"><p class="first admonition-title"><span class="yiyi-st" id="yiyi-44">也可以看看</span></p><dl class="last docutils"><dt><span class="yiyi-st" id="yiyi-45">模块<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></span></dt><dd><span class="yiyi-st" id="yiyi-46">Unix shell样式的路径扩展。</span></dd></dl></div></div></div>