Server : Apache System : Linux server1.cgrithy.com 3.10.0-1160.95.1.el7.x86_64 #1 SMP Mon Jul 24 13:59:37 UTC 2023 x86_64 User : nobody ( 99) PHP Version : 8.1.23 Disable Function : NONE Directory : /usr/share/doc/python-docs-2.7.5/html/library/ |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>10.7. glob — Unix style pathname pattern expansion — Python 2.7.5 documentation</title> <link rel="stylesheet" href="../_static/default.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../', VERSION: '2.7.5', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <script type="text/javascript" src="../_static/sidebar.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python 2.7.5 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="top" title="Python 2.7.5 documentation" href="../index.html" /> <link rel="up" title="10. File and Directory Access" href="filesys.html" /> <link rel="next" title="10.8. fnmatch — Unix filename pattern matching" href="fnmatch.html" /> <link rel="prev" title="10.6. tempfile — Generate temporary files and directories" href="tempfile.html" /> <link rel="shortcut icon" type="image/png" href="../_static/py.png" /> <script type="text/javascript" src="../_static/copybutton.js"></script> </head> <body> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="fnmatch.html" title="10.8. fnmatch — Unix filename pattern matching" accesskey="N">next</a> |</li> <li class="right" > <a href="tempfile.html" title="10.6. tempfile — Generate temporary files and directories" accesskey="P">previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="filesys.html" accesskey="U">10. File and Directory Access</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="module-glob"> <span id="glob-unix-style-pathname-pattern-expansion"></span><h1>10.7. <a class="reference internal" href="#module-glob" title="glob: Unix shell style pathname pattern expansion."><tt class="xref py py-mod docutils literal"><span class="pre">glob</span></tt></a> — Unix style pathname pattern expansion<a class="headerlink" href="#module-glob" title="Permalink to this headline">¶</a></h1> <p id="index-0"><strong>Source code:</strong> <a class="reference external" href="http://hg.python.org/cpython/file/2.7/Lib/glob.py">Lib/glob.py</a></p> <hr class="docutils" /> <p>The <a class="reference internal" href="#module-glob" title="glob: Unix shell style pathname pattern expansion."><tt class="xref py py-mod docutils literal"><span class="pre">glob</span></tt></a> module finds all the pathnames matching a specified pattern according to the rules used by the Unix shell. No tilde expansion is done, but <tt class="docutils literal"><span class="pre">*</span></tt>, <tt class="docutils literal"><span class="pre">?</span></tt>, and character ranges expressed with <tt class="docutils literal"><span class="pre">[]</span></tt> will be correctly matched. This is done by using the <a class="reference internal" href="os.html#os.listdir" title="os.listdir"><tt class="xref py py-func docutils literal"><span class="pre">os.listdir()</span></tt></a> and <a class="reference internal" href="fnmatch.html#fnmatch.fnmatch" title="fnmatch.fnmatch"><tt class="xref py py-func docutils literal"><span class="pre">fnmatch.fnmatch()</span></tt></a> functions in concert, and not by actually invoking a subshell. Note that unlike <a class="reference internal" href="fnmatch.html#fnmatch.fnmatch" title="fnmatch.fnmatch"><tt class="xref py py-func docutils literal"><span class="pre">fnmatch.fnmatch()</span></tt></a>, <a class="reference internal" href="#module-glob" title="glob: Unix shell style pathname pattern expansion."><tt class="xref py py-mod docutils literal"><span class="pre">glob</span></tt></a> treats filenames beginning with a dot (<tt class="docutils literal"><span class="pre">.</span></tt>) as special cases. (For tilde and shell variable expansion, use <a class="reference internal" href="os.path.html#os.path.expanduser" title="os.path.expanduser"><tt class="xref py py-func docutils literal"><span class="pre">os.path.expanduser()</span></tt></a> and <a class="reference internal" href="os.path.html#os.path.expandvars" title="os.path.expandvars"><tt class="xref py py-func docutils literal"><span class="pre">os.path.expandvars()</span></tt></a>.)</p> <p>For a literal match, wrap the meta-characters in brackets. For example, <tt class="docutils literal"><span class="pre">'[?]'</span></tt> matches the character <tt class="docutils literal"><span class="pre">'?'</span></tt>.</p> <dl class="function"> <dt id="glob.glob"> <tt class="descclassname">glob.</tt><tt class="descname">glob</tt><big>(</big><em>pathname</em><big>)</big><a class="headerlink" href="#glob.glob" title="Permalink to this definition">¶</a></dt> <dd><p>Return a possibly-empty list of path names that match <em>pathname</em>, which must be a string containing a path specification. <em>pathname</em> can be either absolute (like <tt class="file docutils literal"><span class="pre">/usr/src/Python-1.5/Makefile</span></tt>) or relative (like <tt class="file docutils literal"><span class="pre">../../Tools/*/*.gif</span></tt>), and can contain shell-style wildcards. Broken symlinks are included in the results (as in the shell).</p> </dd></dl> <dl class="function"> <dt id="glob.iglob"> <tt class="descclassname">glob.</tt><tt class="descname">iglob</tt><big>(</big><em>pathname</em><big>)</big><a class="headerlink" href="#glob.iglob" title="Permalink to this definition">¶</a></dt> <dd><p>Return an <a class="reference internal" href="../glossary.html#term-iterator"><em class="xref std std-term">iterator</em></a> which yields the same values as <a class="reference internal" href="#module-glob" title="glob: Unix shell style pathname pattern expansion."><tt class="xref py py-func docutils literal"><span class="pre">glob()</span></tt></a> without actually storing them all simultaneously.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.5.</span></p> </dd></dl> <p>For example, consider a directory containing only the following files: <tt class="file docutils literal"><span class="pre">1.gif</span></tt>, <tt class="file docutils literal"><span class="pre">2.txt</span></tt>, and <tt class="file docutils literal"><span class="pre">card.gif</span></tt>. <a class="reference internal" href="#module-glob" title="glob: Unix shell style pathname pattern expansion."><tt class="xref py py-func docutils literal"><span class="pre">glob()</span></tt></a> will produce the following results. Notice how any leading components of the path are preserved.</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">glob</span> <span class="gp">>>> </span><span class="n">glob</span><span class="o">.</span><span class="n">glob</span><span class="p">(</span><span class="s">'./[0-9].*'</span><span class="p">)</span> <span class="go">['./1.gif', './2.txt']</span> <span class="gp">>>> </span><span class="n">glob</span><span class="o">.</span><span class="n">glob</span><span class="p">(</span><span class="s">'*.gif'</span><span class="p">)</span> <span class="go">['1.gif', 'card.gif']</span> <span class="gp">>>> </span><span class="n">glob</span><span class="o">.</span><span class="n">glob</span><span class="p">(</span><span class="s">'?.gif'</span><span class="p">)</span> <span class="go">['1.gif']</span> </pre></div> </div> <p>If the directory contains files starting with <tt class="docutils literal"><span class="pre">.</span></tt> they won’t be matched by default. For example, consider a directory containing <tt class="file docutils literal"><span class="pre">card.gif</span></tt> and <tt class="file docutils literal"><span class="pre">.card.gif</span></tt>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">glob</span> <span class="gp">>>> </span><span class="n">glob</span><span class="o">.</span><span class="n">glob</span><span class="p">(</span><span class="s">'*.gif'</span><span class="p">)</span> <span class="go">['card.gif']</span> <span class="gp">>>> </span><span class="n">glob</span><span class="o">.</span><span class="n">glob</span><span class="p">(</span><span class="s">'.c*'</span><span class="p">)</span> <span class="go">['.card.gif']</span> </pre></div> </div> <div class="admonition-see-also admonition seealso"> <p class="first admonition-title">See also</p> <dl class="last docutils"> <dt>Module <a class="reference internal" href="fnmatch.html#module-fnmatch" title="fnmatch: Unix shell style filename pattern matching."><tt class="xref py py-mod docutils literal"><span class="pre">fnmatch</span></tt></a></dt> <dd>Shell-style filename (not path) expansion</dd> </dl> </div> </div> </div> </div> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h4>Previous topic</h4> <p class="topless"><a href="tempfile.html" title="previous chapter">10.6. <tt class="docutils literal"><span class="pre">tempfile</span></tt> — Generate temporary files and directories</a></p> <h4>Next topic</h4> <p class="topless"><a href="fnmatch.html" title="next chapter">10.8. <tt class="docutils literal"><span class="pre">fnmatch</span></tt> — Unix filename pattern matching</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../bugs.html">Report a Bug</a></li> <li><a href="../_sources/library/glob.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="fnmatch.html" title="10.8. fnmatch — Unix filename pattern matching" >next</a> |</li> <li class="right" > <a href="tempfile.html" title="10.6. tempfile — Generate temporary files and directories" >previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="filesys.html" >10. File and Directory Access</a> »</li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 1990-2020, Python Software Foundation. <br /> The Python Software Foundation is a non-profit corporation. <a href="http://www.python.org/psf/donations/">Please donate.</a> <br /> Last updated on Oct 13, 2020. <a href="../bugs.html">Found a bug</a>? <br /> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. </div> </body> </html>