Team Ai
Apppublic

parthtamu/rag-code-assistant

sourceHugging Faceupdated 7mo agoView on Hugging Face
0likes
glob.html542 linesDownload Raw Back to docs
1<!DOCTYPE html>2 3<html lang="en" data-content_root="../">4  <head>5    <meta charset="utf-8" />6    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />7<meta property="og:title" content="glob — Unix style pathname pattern expansion" />8<meta property="og:type" content="website" />9<meta property="og:url" content="https://docs.python.org/3/library/glob.html" />10<meta property="og:site_name" content="Python documentation" />11<meta property="og:description" content="Source code: Lib/glob.py The glob module finds pathnames using pattern matching rules similar to the Unix shell. No tilde expansion is done, but*,?, and character ranges expressed with[] will be co..." />12<meta property="og:image:width" content="1146" />13<meta property="og:image:height" content="600" />14<meta property="og:image" content="https://docs.python.org/3.15/_images/social_previews/summary_library_glob_6360a2ac.png" />15<meta property="og:image:alt" content="Source code: Lib/glob.py The glob module finds pathnames using pattern matching rules similar to the Unix shell. No tilde expansion is done, but*,?, and character ranges expressed with[] will be co..." />16<meta name="description" content="Source code: Lib/glob.py The glob module finds pathnames using pattern matching rules similar to the Unix shell. No tilde expansion is done, but*,?, and character ranges expressed with[] will be co..." />17<meta name="twitter:card" content="summary_large_image" />18<meta name="theme-color" content="#3776ab">19 20    <title>glob — Unix style pathname pattern expansion &#8212; Python 3.15.0a6 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">21    22    <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />23    <link rel="stylesheet" type="text/css" href="../_static/classic.css?v=234b1a7c" />24    <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=89a2f22a" />25    <link rel="stylesheet" type="text/css" href="../_static/profiling-sampling-visualization.css?v=0c2600ae" />26    <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=5349f25f" />27    28    <script src="../_static/documentation_options.js?v=6b7c9ff5"></script>29    <script src="../_static/doctools.js?v=9bcbadda"></script>30    <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>31    <script src="../_static/profiling-sampling-visualization.js?v=9811ed04"></script>32    33    <script src="../_static/sidebar.js"></script>34    35    <link rel="search" type="application/opensearchdescription+xml"36          title="Search within Python 3.15.0a6 documentation"37          href="../_static/opensearch.xml"/>38    <link rel="author" title="About these documents" href="../about.html" />39    <link rel="index" title="Index" href="../genindex.html" />40    <link rel="search" title="Search" href="../search.html" />41    <link rel="copyright" title="Copyright" href="../copyright.html" />42    <link rel="next" title="fnmatch — Unix filename pattern matching" href="fnmatch.html" />43    <link rel="prev" title="tempfile — Generate temporary files and directories" href="tempfile.html" />44    45      46      <script defer file-types="bz2,epub,zip" data-domain="docs.python.org" src="https://analytics.python.org/js/script.file-downloads.outbound-links.js"></script>47      48      <link rel="canonical" href="https://docs.python.org/3/library/glob.html">49      50    51 52    53    <style>54      @media only screen {55        table.full-width-table {56            width: 100%;57        }58      }59    </style>60<link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">61    <link rel="shortcut icon" type="image/png" href="../_static/py.svg">62            <script type="text/javascript" src="../_static/copybutton.js"></script>63            <script type="text/javascript" src="../_static/menu.js"></script>64            <script type="text/javascript" src="../_static/search-focus.js"></script>65            <script type="text/javascript" src="../_static/themetoggle.js"></script> 66            <script type="text/javascript" src="../_static/rtd_switcher.js"></script>67            <meta name="readthedocs-addons-api-version" content="1">68 69  </head>70<body>71<div class="mobile-nav">72    <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"73           aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu">74    <nav class="nav-content" role="navigation">75        <label for="menuToggler" class="toggler__label">76            <span></span>77        </label>78        <span class="nav-items-wrapper">79            <a href="https://www.python.org/" class="nav-logo">80                <img src="../_static/py.svg" alt="Python logo">81            </a>82            <span class="version_switcher_placeholder"></span>83            <form role="search" class="search" action="../search.html" method="get">84                <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">85                    <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>86                </svg>87                <input placeholder="Quick search" aria-label="Quick search" type="search" name="q">88                <input type="submit" value="Go">89            </form>90        </span>91    </nav>92    <div class="menu-wrapper">93        <nav class="menu" role="navigation" aria-label="main navigation">94            <div class="language_switcher_placeholder"></div>95            96<label class="theme-selector-label">97    Theme98    <select class="theme-selector" oninput="activateTheme(this.value)">99        <option value="auto" selected>Auto</option>100        <option value="light">Light</option>101        <option value="dark">Dark</option>102    </select>103</label>104  <div>105    <h3><a href="../contents.html">Table of Contents</a></h3>106    <ul>107<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">glob</span></code> — Unix style pathname pattern expansion</a><ul>108<li><a class="reference internal" href="#examples">Examples</a></li>109</ul>110</li>111</ul>112 113  </div>114  <div>115    <h4>Previous topic</h4>116    <p class="topless"><a href="tempfile.html"117                          title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tempfile</span></code> — Generate temporary files and directories</a></p>118  </div>119  <div>120    <h4>Next topic</h4>121    <p class="topless"><a href="fnmatch.html"122                          title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">fnmatch</span></code> — Unix filename pattern matching</a></p>123  </div>124  <script>125    document.addEventListener('DOMContentLoaded', () => {126        const title = document.querySelector('meta[property="og:title"]').content;127        const elements = document.querySelectorAll('.improvepage');128        const pageurl = window.location.href.split('?')[0];129        elements.forEach(element => {130            const url = new URL(element.href.split('?')[0].replace("-nojs", ""));131            url.searchParams.set('pagetitle', title);132            url.searchParams.set('pageurl', pageurl);133            url.searchParams.set('pagesource', "library/glob.rst");134            element.href = url.toString();135        });136    });137  </script>138  <div role="note" aria-label="source link">139    <h3>This page</h3>140    <ul class="this-page-menu">141      <li><a href="../bugs.html">Report a bug</a></li>142      <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>143      <li>144        <a href="https://github.com/python/cpython/blob/main/Doc/library/glob.rst?plain=1"145            rel="nofollow">Show source146        </a>147      </li>148      149    </ul>150  </div>151        </nav>152    </div>153</div>154 155  156    <div class="related" role="navigation" aria-label="Related">157      <h3>Navigation</h3>158      <ul>159        <li class="right" style="margin-right: 10px">160          <a href="../genindex.html" title="General Index"161             accesskey="I">index</a></li>162        <li class="right" >163          <a href="../py-modindex.html" title="Python Module Index"164             >modules</a> |</li>165        <li class="right" >166          <a href="fnmatch.html" title="fnmatch — Unix filename pattern matching"167             accesskey="N">next</a> |</li>168        <li class="right" >169          <a href="tempfile.html" title="tempfile — Generate temporary files and directories"170             accesskey="P">previous</a> |</li>171 172          <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>173          <li><a href="https://www.python.org/">Python</a> &#187;</li>174          <li class="switchers">175            <div class="language_switcher_placeholder"></div>176            <div class="version_switcher_placeholder"></div>177          </li>178          <li>179              180          </li>181    <li id="cpython-language-and-version">182      <a href="../index.html">3.15.0a6 Documentation</a> &#187;183    </li>184 185          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>186          <li class="nav-item nav-item-2"><a href="filesys.html" accesskey="U">File and Directory Access</a> &#187;</li>187        <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">glob</span></code> — Unix style pathname pattern expansion</a></li>188                <li class="right">189                    190 191    <div class="inline-search" role="search">192        <form class="inline-search" action="../search.html" method="get">193          <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">194          <input type="submit" value="Go">195        </form>196    </div>197                     |198                </li>199            <li class="right">200<label class="theme-selector-label">201    Theme202    <select class="theme-selector" oninput="activateTheme(this.value)">203        <option value="auto" selected>Auto</option>204        <option value="light">Light</option>205        <option value="dark">Dark</option>206    </select>207</label> |</li>208            209      </ul>210    </div>    211 212    <div class="document">213      <div class="documentwrapper">214        <div class="bodywrapper">215          <div class="body" role="main">216            217  <section id="module-glob">218<span id="glob-unix-style-pathname-pattern-expansion"></span><h1><code class="xref py py-mod docutils literal notranslate"><span class="pre">glob</span></code> — Unix style pathname pattern expansion<a class="headerlink" href="#module-glob" title="Link to this heading">¶</a></h1>219<p><strong>Source code:</strong> <a class="extlink-source reference external" href="https://github.com/python/cpython/tree/main/Lib/glob.py">Lib/glob.py</a></p>220<hr class="docutils" id="index-0" />221<p id="index-1">The <code class="xref py py-mod docutils literal notranslate"><span class="pre">glob</span></code> module finds pathnames222using pattern matching rules similar to the Unix shell.223No tilde expansion is done, but <code class="docutils literal notranslate"><span class="pre">*</span></code>, <code class="docutils literal notranslate"><span class="pre">?</span></code>, and character224ranges expressed with <code class="docutils literal notranslate"><span class="pre">[]</span></code> will be correctly matched.  This is done by using225the <a class="reference internal" href="os.html#os.scandir" title="os.scandir"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.scandir()</span></code></a> and <a class="reference internal" href="fnmatch.html#fnmatch.fnmatch" title="fnmatch.fnmatch"><code class="xref py py-func docutils literal notranslate"><span class="pre">fnmatch.fnmatch()</span></code></a> functions in concert, and226not by actually invoking a subshell.</p>227<div class="admonition note">228<p class="admonition-title">Note</p>229<p>The pathnames are returned in no particular order.  If you need a specific230order, sort the results.</p>231</div>232<p>Files beginning with a dot (<code class="docutils literal notranslate"><span class="pre">.</span></code>) can only be matched by233patterns that also start with a dot,234unlike <a class="reference internal" href="fnmatch.html#fnmatch.fnmatch" title="fnmatch.fnmatch"><code class="xref py py-func docutils literal notranslate"><span class="pre">fnmatch.fnmatch()</span></code></a> or <a class="reference internal" href="pathlib.html#pathlib.Path.glob" title="pathlib.Path.glob"><code class="xref py py-func docutils literal notranslate"><span class="pre">pathlib.Path.glob()</span></code></a>.235For tilde and shell variable expansion, use <a class="reference internal" href="os.path.html#os.path.expanduser" title="os.path.expanduser"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.path.expanduser()</span></code></a> and236<a class="reference internal" href="os.path.html#os.path.expandvars" title="os.path.expandvars"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.path.expandvars()</span></code></a>.</p>237<p>For a literal match, wrap the meta-characters in brackets.238For example, <code class="docutils literal notranslate"><span class="pre">'[?]'</span></code> matches the character <code class="docutils literal notranslate"><span class="pre">'?'</span></code>.</p>239<p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">glob</span></code> module defines the following functions:</p>240<dl class="py function">241<dt class="sig sig-object py" id="glob.glob">242<span class="sig-prename descclassname"><span class="pre">glob.</span></span><span class="sig-name descname"><span class="pre">glob</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">pathname</span></span></em>, <em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">root_dir</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dir_fd</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">recursive</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">include_hidden</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#glob.glob" title="Link to this definition">¶</a></dt>243<dd><p>Return a possibly empty list of path names that match <em>pathname</em>, which must be244a string containing a path specification. <em>pathname</em> can be either absolute245(like <code class="file docutils literal notranslate"><span class="pre">/usr/src/Python-1.5/Makefile</span></code>) or relative (like246<code class="file docutils literal notranslate"><span class="pre">../../Tools/*/*.gif</span></code>), and can contain shell-style wildcards. Broken247symlinks are included in the results (as in the shell). Whether or not the248results are sorted depends on the file system.  If a file that satisfies249conditions is removed or added during the call of this function, whether250a path name for that file will be included is unspecified.</p>251<p>If <em>root_dir</em> is not <code class="docutils literal notranslate"><span class="pre">None</span></code>, it should be a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>252specifying the root directory for searching.  It has the same effect on253<code class="xref py py-func docutils literal notranslate"><span class="pre">glob()</span></code> as changing the current directory before calling it.  If254<em>pathname</em> is relative, the result will contain paths relative to255<em>root_dir</em>.</p>256<p>This function can support <a class="reference internal" href="os.html#dir-fd"><span class="std std-ref">paths relative to directory descriptors</span></a> with the <em>dir_fd</em> parameter.</p>257<p id="index-2">If <em>recursive</em> is true, the pattern “<code class="docutils literal notranslate"><span class="pre">**</span></code>” will match any files and zero or258more directories, subdirectories and symbolic links to directories. If the259pattern is followed by an <a class="reference internal" href="os.html#os.sep" title="os.sep"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.sep</span></code></a> or <a class="reference internal" href="os.html#os.altsep" title="os.altsep"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.altsep</span></code></a> then files will not260match.</p>261<p>If <em>include_hidden</em> is true, “<code class="docutils literal notranslate"><span class="pre">**</span></code>” pattern will match hidden directories.</p>262<p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">glob.glob</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">pathname</span></code>, <code class="docutils literal notranslate"><span class="pre">recursive</span></code>.</p>263<p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">glob.glob/2</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">pathname</span></code>, <code class="docutils literal notranslate"><span class="pre">recursive</span></code>, <code class="docutils literal notranslate"><span class="pre">root_dir</span></code>, <code class="docutils literal notranslate"><span class="pre">dir_fd</span></code>.</p>264<div class="admonition note">265<p class="admonition-title">Note</p>266<p>Using the “<code class="docutils literal notranslate"><span class="pre">**</span></code>” pattern in large directory trees may consume267an inordinate amount of time.</p>268</div>269<div class="admonition note">270<p class="admonition-title">Note</p>271<p>This function may return duplicate path names if <em>pathname</em>272contains multiple “<code class="docutils literal notranslate"><span class="pre">**</span></code>” patterns and <em>recursive</em> is true.</p>273</div>274<div class="versionchanged">275<p><span class="versionmodified changed">Changed in version 3.5: </span>Support for recursive globs using “<code class="docutils literal notranslate"><span class="pre">**</span></code>”.</p>276</div>277<div class="versionchanged">278<p><span class="versionmodified changed">Changed in version 3.10: </span>Added the <em>root_dir</em> and <em>dir_fd</em> parameters.</p>279</div>280<div class="versionchanged">281<p><span class="versionmodified changed">Changed in version 3.11: </span>Added the <em>include_hidden</em> parameter.</p>282</div>283</dd></dl>284 285<dl class="py function">286<dt class="sig sig-object py" id="glob.iglob">287<span class="sig-prename descclassname"><span class="pre">glob.</span></span><span class="sig-name descname"><span class="pre">iglob</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">pathname</span></span></em>, <em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">root_dir</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dir_fd</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">recursive</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">include_hidden</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#glob.iglob" title="Link to this definition">¶</a></dt>288<dd><p>Return an <a class="reference internal" href="../glossary.html#term-iterator"><span class="xref std std-term">iterator</span></a> which yields the same values as <a class="reference internal" href="#module-glob" title="glob: Unix shell style pathname pattern expansion."><code class="xref py py-func docutils literal notranslate"><span class="pre">glob()</span></code></a>289without actually storing them all simultaneously.</p>290<p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">glob.glob</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">pathname</span></code>, <code class="docutils literal notranslate"><span class="pre">recursive</span></code>.</p>291<p class="audit-hook">Raises an <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">auditing event</span></a> <code class="docutils literal notranslate"><span class="pre">glob.glob/2</span></code> with arguments <code class="docutils literal notranslate"><span class="pre">pathname</span></code>, <code class="docutils literal notranslate"><span class="pre">recursive</span></code>, <code class="docutils literal notranslate"><span class="pre">root_dir</span></code>, <code class="docutils literal notranslate"><span class="pre">dir_fd</span></code>.</p>292<div class="admonition note">293<p class="admonition-title">Note</p>294<p>This function may return duplicate path names if <em>pathname</em>295contains multiple “<code class="docutils literal notranslate"><span class="pre">**</span></code>” patterns and <em>recursive</em> is true.</p>296</div>297<div class="versionchanged">298<p><span class="versionmodified changed">Changed in version 3.5: </span>Support for recursive globs using “<code class="docutils literal notranslate"><span class="pre">**</span></code>”.</p>299</div>300<div class="versionchanged">301<p><span class="versionmodified changed">Changed in version 3.10: </span>Added the <em>root_dir</em> and <em>dir_fd</em> parameters.</p>302</div>303<div class="versionchanged">304<p><span class="versionmodified changed">Changed in version 3.11: </span>Added the <em>include_hidden</em> parameter.</p>305</div>306</dd></dl>307 308<dl class="py function">309<dt class="sig sig-object py" id="glob.escape">310<span class="sig-prename descclassname"><span class="pre">glob.</span></span><span class="sig-name descname"><span class="pre">escape</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">pathname</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#glob.escape" title="Link to this definition">¶</a></dt>311<dd><p>Escape all special characters (<code class="docutils literal notranslate"><span class="pre">'?'</span></code>, <code class="docutils literal notranslate"><span class="pre">'*'</span></code> and <code class="docutils literal notranslate"><span class="pre">'['</span></code>).312This is useful if you want to match an arbitrary literal string that may313have special characters in it.  Special characters in drive/UNC314sharepoints are not escaped, e.g. on Windows315<code class="docutils literal notranslate"><span class="pre">escape('//?/c:/Quo</span> <span class="pre">vadis?.txt')</span></code> returns <code class="docutils literal notranslate"><span class="pre">'//?/c:/Quo</span> <span class="pre">vadis[?].txt'</span></code>.</p>316<div class="versionadded">317<p><span class="versionmodified added">Added in version 3.4.</span></p>318</div>319</dd></dl>320 321<dl class="py function">322<dt class="sig sig-object py" id="glob.translate">323<span class="sig-prename descclassname"><span class="pre">glob.</span></span><span class="sig-name descname"><span class="pre">translate</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">pathname</span></span></em>, <em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">recursive</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">include_hidden</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">seps</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#glob.translate" title="Link to this definition">¶</a></dt>324<dd><p>Convert the given path specification to a regular expression for use with325<a class="reference internal" href="re.html#re.match" title="re.match"><code class="xref py py-func docutils literal notranslate"><span class="pre">re.match()</span></code></a>. The path specification can contain shell-style wildcards.</p>326<p>For example:</p>327<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span><span class="w"> </span><span class="nn">glob</span><span class="o">,</span><span class="w"> </span><span class="nn">re</span>328<span class="gp">&gt;&gt;&gt;</span>329<span class="gp">&gt;&gt;&gt; </span><span class="n">regex</span> <span class="o">=</span> <span class="n">glob</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> <span class="n">recursive</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">include_hidden</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>330<span class="gp">&gt;&gt;&gt; </span><span class="n">regex</span>331<span class="go">&#39;(?s:(?:.+/)?[^/]*\\.txt)\\z&#39;</span>332<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>333<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;foo/bar/baz.txt&#39;</span><span class="p">)</span>334<span class="go">&lt;re.Match object; span=(0, 15), match=&#39;foo/bar/baz.txt&#39;&gt;</span>335</pre></div>336</div>337<p>Path separators and segments are meaningful to this function, unlike338<a class="reference internal" href="fnmatch.html#fnmatch.translate" title="fnmatch.translate"><code class="xref py py-func docutils literal notranslate"><span class="pre">fnmatch.translate()</span></code></a>. By default wildcards do not match path339separators, and <code class="docutils literal notranslate"><span class="pre">*</span></code> pattern segments match precisely one path segment.</p>340<p>If <em>recursive</em> is true, the pattern segment “<code class="docutils literal notranslate"><span class="pre">**</span></code>” will match any number341of path segments.</p>342<p>If <em>include_hidden</em> is true, wildcards can match path segments that start343with a dot (<code class="docutils literal notranslate"><span class="pre">.</span></code>).</p>344<p>A sequence of path separators may be supplied to the <em>seps</em> argument. If345not given, <a class="reference internal" href="os.html#os.sep" title="os.sep"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.sep</span></code></a> and <a class="reference internal" href="os.html#os.altsep" title="os.altsep"><code class="xref py py-data docutils literal notranslate"><span class="pre">altsep</span></code></a> (if available) are used.</p>346<div class="admonition seealso">347<p class="admonition-title">See also</p>348<p><a class="reference internal" href="pathlib.html#pathlib.PurePath.full_match" title="pathlib.PurePath.full_match"><code class="xref py py-meth docutils literal notranslate"><span class="pre">pathlib.PurePath.full_match()</span></code></a> and <a class="reference internal" href="pathlib.html#pathlib.Path.glob" title="pathlib.Path.glob"><code class="xref py py-meth docutils literal notranslate"><span class="pre">pathlib.Path.glob()</span></code></a>349methods, which call this function to implement pattern matching and350globbing.</p>351</div>352<div class="versionadded">353<p><span class="versionmodified added">Added in version 3.13.</span></p>354</div>355</dd></dl>356 357<section id="examples">358<h2>Examples<a class="headerlink" href="#examples" title="Link to this heading">¶</a></h2>359<p>Consider a directory containing the following files:360<code class="file docutils literal notranslate"><span class="pre">1.gif</span></code>, <code class="file docutils literal notranslate"><span class="pre">2.txt</span></code>, <code class="file docutils literal notranslate"><span class="pre">card.gif</span></code> and a subdirectory <code class="file docutils literal notranslate"><span class="pre">sub</span></code>361which contains only the file <code class="file docutils literal notranslate"><span class="pre">3.txt</span></code>.  <a class="reference internal" href="#module-glob" title="glob: Unix shell style pathname pattern expansion."><code class="xref py py-func docutils literal notranslate"><span class="pre">glob()</span></code></a> will produce362the following results.  Notice how any leading components of the path are363preserved.</p>364<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span><span class="w"> </span><span class="nn">glob</span>365<span class="gp">&gt;&gt;&gt; </span><span class="n">glob</span><span class="o">.</span><span class="n">glob</span><span class="p">(</span><span class="s1">&#39;./[0-9].*&#39;</span><span class="p">)</span>366<span class="go">[&#39;./1.gif&#39;, &#39;./2.txt&#39;]</span>367<span class="gp">&gt;&gt;&gt; </span><span class="n">glob</span><span class="o">.</span><span class="n">glob</span><span class="p">(</span><span class="s1">&#39;*.gif&#39;</span><span class="p">)</span>368<span class="go">[&#39;1.gif&#39;, &#39;card.gif&#39;]</span>369<span class="gp">&gt;&gt;&gt; </span><span class="n">glob</span><span class="o">.</span><span class="n">glob</span><span class="p">(</span><span class="s1">&#39;?.gif&#39;</span><span class="p">)</span>370<span class="go">[&#39;1.gif&#39;]</span>371<span class="gp">&gt;&gt;&gt; </span><span class="n">glob</span><span class="o">.</span><span class="n">glob</span><span class="p">(</span><span class="s1">&#39;**/*.txt&#39;</span><span class="p">,</span> <span class="n">recursive</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>372<span class="go">[&#39;2.txt&#39;, &#39;sub/3.txt&#39;]</span>373<span class="gp">&gt;&gt;&gt; </span><span class="n">glob</span><span class="o">.</span><span class="n">glob</span><span class="p">(</span><span class="s1">&#39;./**/&#39;</span><span class="p">,</span> <span class="n">recursive</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>374<span class="go">[&#39;./&#39;, &#39;./sub/&#39;]</span>375</pre></div>376</div>377<p>If the directory contains files starting with <code class="docutils literal notranslate"><span class="pre">.</span></code> they won’t be matched by378default. For example, consider a directory containing <code class="file docutils literal notranslate"><span class="pre">card.gif</span></code> and379<code class="file docutils literal notranslate"><span class="pre">.card.gif</span></code>:</p>380<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span><span class="w"> </span><span class="nn">glob</span>381<span class="gp">&gt;&gt;&gt; </span><span class="n">glob</span><span class="o">.</span><span class="n">glob</span><span class="p">(</span><span class="s1">&#39;*.gif&#39;</span><span class="p">)</span>382<span class="go">[&#39;card.gif&#39;]</span>383<span class="gp">&gt;&gt;&gt; </span><span class="n">glob</span><span class="o">.</span><span class="n">glob</span><span class="p">(</span><span class="s1">&#39;.c*&#39;</span><span class="p">)</span>384<span class="go">[&#39;.card.gif&#39;]</span>385</pre></div>386</div>387<div class="admonition seealso">388<p class="admonition-title">See also</p>389<p>The <a class="reference internal" href="fnmatch.html#module-fnmatch" title="fnmatch: Unix shell style filename pattern matching."><code class="xref py py-mod docutils literal notranslate"><span class="pre">fnmatch</span></code></a> module offers shell-style filename (not path) expansion.</p>390</div>391<div class="admonition seealso">392<p class="admonition-title">See also</p>393<p>The <a class="reference internal" href="pathlib.html#module-pathlib" title="pathlib: Object-oriented filesystem paths"><code class="xref py py-mod docutils literal notranslate"><span class="pre">pathlib</span></code></a> module offers high-level path objects.</p>394</div>395</section>396</section>397 398 399            <div class="clearer"></div>400          </div>401        </div>402      </div>403      <div class="sphinxsidebar" role="navigation" aria-label="Main">404        <div class="sphinxsidebarwrapper">405  <div>406    <h3><a href="../contents.html">Table of Contents</a></h3>407    <ul>408<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">glob</span></code> — Unix style pathname pattern expansion</a><ul>409<li><a class="reference internal" href="#examples">Examples</a></li>410</ul>411</li>412</ul>413 414  </div>415  <div>416    <h4>Previous topic</h4>417    <p class="topless"><a href="tempfile.html"418                          title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tempfile</span></code> — Generate temporary files and directories</a></p>419  </div>420  <div>421    <h4>Next topic</h4>422    <p class="topless"><a href="fnmatch.html"423                          title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">fnmatch</span></code> — Unix filename pattern matching</a></p>424  </div>425  <script>426    document.addEventListener('DOMContentLoaded', () => {427        const title = document.querySelector('meta[property="og:title"]').content;428        const elements = document.querySelectorAll('.improvepage');429        const pageurl = window.location.href.split('?')[0];430        elements.forEach(element => {431            const url = new URL(element.href.split('?')[0].replace("-nojs", ""));432            url.searchParams.set('pagetitle', title);433            url.searchParams.set('pageurl', pageurl);434            url.searchParams.set('pagesource', "library/glob.rst");435            element.href = url.toString();436        });437    });438  </script>439  <div role="note" aria-label="source link">440    <h3>This page</h3>441    <ul class="this-page-menu">442      <li><a href="../bugs.html">Report a bug</a></li>443      <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>444      <li>445        <a href="https://github.com/python/cpython/blob/main/Doc/library/glob.rst?plain=1"446            rel="nofollow">Show source447        </a>448      </li>449      450    </ul>451  </div>452        </div>453<div id="sidebarbutton" title="Collapse sidebar">454<span>«</span>455</div>456 457      </div>458      <div class="clearer"></div>459    </div>  460    <div class="related" role="navigation" aria-label="Related">461      <h3>Navigation</h3>462      <ul>463        <li class="right" style="margin-right: 10px">464          <a href="../genindex.html" title="General Index"465             >index</a></li>466        <li class="right" >467          <a href="../py-modindex.html" title="Python Module Index"468             >modules</a> |</li>469        <li class="right" >470          <a href="fnmatch.html" title="fnmatch — Unix filename pattern matching"471             >next</a> |</li>472        <li class="right" >473          <a href="tempfile.html" title="tempfile — Generate temporary files and directories"474             >previous</a> |</li>475 476          <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>477          <li><a href="https://www.python.org/">Python</a> &#187;</li>478          <li class="switchers">479            <div class="language_switcher_placeholder"></div>480            <div class="version_switcher_placeholder"></div>481          </li>482          <li>483              484          </li>485    <li id="cpython-language-and-version">486      <a href="../index.html">3.15.0a6 Documentation</a> &#187;487    </li>488 489          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>490          <li class="nav-item nav-item-2"><a href="filesys.html" >File and Directory Access</a> &#187;</li>491        <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">glob</span></code> — Unix style pathname pattern expansion</a></li>492                <li class="right">493                    494 495    <div class="inline-search" role="search">496        <form class="inline-search" action="../search.html" method="get">497          <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">498          <input type="submit" value="Go">499        </form>500    </div>501                     |502                </li>503            <li class="right">504<label class="theme-selector-label">505    Theme506    <select class="theme-selector" oninput="activateTheme(this.value)">507        <option value="auto" selected>Auto</option>508        <option value="light">Light</option>509        <option value="dark">Dark</option>510    </select>511</label> |</li>512            513      </ul>514    </div>  515    <div class="footer">516    &copy; <a href="../copyright.html">Copyright</a> 2001 Python Software Foundation.517    <br>518    This page is licensed under the Python Software Foundation License Version 2.519    <br>520    Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.521    <br>522    523      See <a href="/license.html">History and License</a> for more information.<br>524    525    526    <br>527 528    The Python Software Foundation is a non-profit corporation.529<a href="https://www.python.org/psf/donations/">Please donate.</a>530<br>531    <br>532      Last updated on Mar 10, 2026 (08:58 UTC).533    534      <a href="/bugs.html">Found a bug</a>?535    536    <br>537 538    Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 8.2.3.539    </div>540 541  </body>542</html>