gooderp18绿色标准版
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

435 lignes
31KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="glob — Unix style pathname pattern expansion" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/glob.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/glob.py The glob module finds all the pathnames matching a specified pattern according to the rules used by the Unix shell, although results are returned in arbitrary order. No til..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="Source code: Lib/glob.py The glob module finds all the pathnames matching a specified pattern according to the rules used by the Unix shell, although results are returned in arbitrary order. No til..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>glob — Unix style pathname pattern expansion &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="fnmatch — Unix filename pattern matching" href="fnmatch.html" />
  33. <link rel="prev" title="tempfile — Generate temporary files and directories" href="tempfile.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/glob.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <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>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h4>Previous topic</h4>
  84. <p class="topless"><a href="tempfile.html"
  85. 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>
  86. </div>
  87. <div>
  88. <h4>Next topic</h4>
  89. <p class="topless"><a href="fnmatch.html"
  90. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">fnmatch</span></code> — Unix filename pattern matching</a></p>
  91. </div>
  92. <div role="note" aria-label="source link">
  93. <h3>This Page</h3>
  94. <ul class="this-page-menu">
  95. <li><a href="../bugs.html">Report a Bug</a></li>
  96. <li>
  97. <a href="https://github.com/python/cpython/blob/main/Doc/library/glob.rst"
  98. rel="nofollow">Show Source
  99. </a>
  100. </li>
  101. </ul>
  102. </div>
  103. </nav>
  104. </div>
  105. </div>
  106. <div class="related" role="navigation" aria-label="related navigation">
  107. <h3>Navigation</h3>
  108. <ul>
  109. <li class="right" style="margin-right: 10px">
  110. <a href="../genindex.html" title="General Index"
  111. accesskey="I">index</a></li>
  112. <li class="right" >
  113. <a href="../py-modindex.html" title="Python Module Index"
  114. >modules</a> |</li>
  115. <li class="right" >
  116. <a href="fnmatch.html" title="fnmatch — Unix filename pattern matching"
  117. accesskey="N">next</a> |</li>
  118. <li class="right" >
  119. <a href="tempfile.html" title="tempfile — Generate temporary files and directories"
  120. accesskey="P">previous</a> |</li>
  121. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  122. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  123. <li class="switchers">
  124. <div class="language_switcher_placeholder"></div>
  125. <div class="version_switcher_placeholder"></div>
  126. </li>
  127. <li>
  128. </li>
  129. <li id="cpython-language-and-version">
  130. <a href="../index.html">3.12.3 Documentation</a> &#187;
  131. </li>
  132. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  133. <li class="nav-item nav-item-2"><a href="filesys.html" accesskey="U">File and Directory Access</a> &#187;</li>
  134. <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>
  135. <li class="right">
  136. <div class="inline-search" role="search">
  137. <form class="inline-search" action="../search.html" method="get">
  138. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  139. <input type="submit" value="Go" />
  140. </form>
  141. </div>
  142. |
  143. </li>
  144. <li class="right">
  145. <label class="theme-selector-label">
  146. Theme
  147. <select class="theme-selector" oninput="activateTheme(this.value)">
  148. <option value="auto" selected>Auto</option>
  149. <option value="light">Light</option>
  150. <option value="dark">Dark</option>
  151. </select>
  152. </label> |</li>
  153. </ul>
  154. </div>
  155. <div class="document">
  156. <div class="documentwrapper">
  157. <div class="bodywrapper">
  158. <div class="body" role="main">
  159. <section id="module-glob">
  160. <span id="glob-unix-style-pathname-pattern-expansion"></span><h1><a class="reference internal" href="#module-glob" title="glob: Unix shell style pathname pattern expansion."><code class="xref py py-mod docutils literal notranslate"><span class="pre">glob</span></code></a> — Unix style pathname pattern expansion<a class="headerlink" href="#module-glob" title="Link to this heading">¶</a></h1>
  161. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/glob.py">Lib/glob.py</a></p>
  162. <hr class="docutils" id="index-0" />
  163. <p id="index-1">The <a class="reference internal" href="#module-glob" title="glob: Unix shell style pathname pattern expansion."><code class="xref py py-mod docutils literal notranslate"><span class="pre">glob</span></code></a> module finds all the pathnames matching a specified pattern
  164. according to the rules used by the Unix shell, although results are returned in
  165. arbitrary order. No 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 character
  166. ranges expressed with <code class="docutils literal notranslate"><span class="pre">[]</span></code> will be correctly matched. This is done by using
  167. the <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, and
  168. not by actually invoking a subshell.</p>
  169. <p>Note that files beginning with a dot (<code class="docutils literal notranslate"><span class="pre">.</span></code>) can only be matched by
  170. patterns that also start with a dot,
  171. unlike <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>.
  172. (For 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> and
  173. <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>
  174. <p>For a literal match, wrap the meta-characters in brackets.
  175. For 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>
  176. <div class="admonition seealso">
  177. <p class="admonition-title">See also</p>
  178. <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>
  179. </div>
  180. <dl class="py function">
  181. <dt class="sig sig-object py" id="glob.glob">
  182. <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="o"><span class="pre">*</span></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>
  183. <dd><p>Return a possibly empty list of path names that match <em>pathname</em>, which must be
  184. a string containing a path specification. <em>pathname</em> can be either absolute
  185. (like <code class="file docutils literal notranslate"><span class="pre">/usr/src/Python-1.5/Makefile</span></code>) or relative (like
  186. <code class="file docutils literal notranslate"><span class="pre">../../Tools/*/*.gif</span></code>), and can contain shell-style wildcards. Broken
  187. symlinks are included in the results (as in the shell). Whether or not the
  188. results are sorted depends on the file system. If a file that satisfies
  189. conditions is removed or added during the call of this function, whether
  190. a path name for that file will be included is unspecified.</p>
  191. <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>
  192. specifying the root directory for searching. It has the same effect on
  193. <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> as changing the current directory before calling it. If
  194. <em>pathname</em> is relative, the result will contain paths relative to
  195. <em>root_dir</em>.</p>
  196. <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>
  197. <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 or
  198. more directories, subdirectories and symbolic links to directories. If the
  199. pattern 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 not
  200. match.</p>
  201. <p>If <em>include_hidden</em> is true, “<code class="docutils literal notranslate"><span class="pre">**</span></code>” pattern will match hidden directories.</p>
  202. <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>
  203. <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>
  204. <div class="admonition note">
  205. <p class="admonition-title">Note</p>
  206. <p>Using the “<code class="docutils literal notranslate"><span class="pre">**</span></code>” pattern in large directory trees may consume
  207. an inordinate amount of time.</p>
  208. </div>
  209. <div class="versionchanged">
  210. <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>
  211. </div>
  212. <div class="versionchanged">
  213. <p><span class="versionmodified changed">Changed in version 3.10: </span>Added the <em>root_dir</em> and <em>dir_fd</em> parameters.</p>
  214. </div>
  215. <div class="versionchanged">
  216. <p><span class="versionmodified changed">Changed in version 3.11: </span>Added the <em>include_hidden</em> parameter.</p>
  217. </div>
  218. </dd></dl>
  219. <dl class="py function">
  220. <dt class="sig sig-object py" id="glob.iglob">
  221. <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="o"><span class="pre">*</span></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>
  222. <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>
  223. without actually storing them all simultaneously.</p>
  224. <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>
  225. <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>
  226. <div class="versionchanged">
  227. <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>
  228. </div>
  229. <div class="versionchanged">
  230. <p><span class="versionmodified changed">Changed in version 3.10: </span>Added the <em>root_dir</em> and <em>dir_fd</em> parameters.</p>
  231. </div>
  232. <div class="versionchanged">
  233. <p><span class="versionmodified changed">Changed in version 3.11: </span>Added the <em>include_hidden</em> parameter.</p>
  234. </div>
  235. </dd></dl>
  236. <dl class="py function">
  237. <dt class="sig sig-object py" id="glob.escape">
  238. <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>
  239. <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>).
  240. This is useful if you want to match an arbitrary literal string that may
  241. have special characters in it. Special characters in drive/UNC
  242. sharepoints are not escaped, e.g. on Windows
  243. <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>
  244. <div class="versionadded">
  245. <p><span class="versionmodified added">New in version 3.4.</span></p>
  246. </div>
  247. </dd></dl>
  248. <p>For example, consider a directory containing the following files:
  249. <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>
  250. which 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 produce
  251. the following results. Notice how any leading components of the path are
  252. preserved.</p>
  253. <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="nn">glob</span>
  254. <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>
  255. <span class="go">[&#39;./1.gif&#39;, &#39;./2.txt&#39;]</span>
  256. <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>
  257. <span class="go">[&#39;1.gif&#39;, &#39;card.gif&#39;]</span>
  258. <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>
  259. <span class="go">[&#39;1.gif&#39;]</span>
  260. <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>
  261. <span class="go">[&#39;2.txt&#39;, &#39;sub/3.txt&#39;]</span>
  262. <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>
  263. <span class="go">[&#39;./&#39;, &#39;./sub/&#39;]</span>
  264. </pre></div>
  265. </div>
  266. <p>If the directory contains files starting with <code class="docutils literal notranslate"><span class="pre">.</span></code> they won’t be matched by
  267. default. For example, consider a directory containing <code class="file docutils literal notranslate"><span class="pre">card.gif</span></code> and
  268. <code class="file docutils literal notranslate"><span class="pre">.card.gif</span></code>:</p>
  269. <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="nn">glob</span>
  270. <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>
  271. <span class="go">[&#39;card.gif&#39;]</span>
  272. <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>
  273. <span class="go">[&#39;.card.gif&#39;]</span>
  274. </pre></div>
  275. </div>
  276. <div class="admonition seealso">
  277. <p class="admonition-title">See also</p>
  278. <dl class="simple">
  279. <dt>Module <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></dt><dd><p>Shell-style filename (not path) expansion</p>
  280. </dd>
  281. </dl>
  282. </div>
  283. </section>
  284. <div class="clearer"></div>
  285. </div>
  286. </div>
  287. </div>
  288. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  289. <div class="sphinxsidebarwrapper">
  290. <div>
  291. <h4>Previous topic</h4>
  292. <p class="topless"><a href="tempfile.html"
  293. 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>
  294. </div>
  295. <div>
  296. <h4>Next topic</h4>
  297. <p class="topless"><a href="fnmatch.html"
  298. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">fnmatch</span></code> — Unix filename pattern matching</a></p>
  299. </div>
  300. <div role="note" aria-label="source link">
  301. <h3>This Page</h3>
  302. <ul class="this-page-menu">
  303. <li><a href="../bugs.html">Report a Bug</a></li>
  304. <li>
  305. <a href="https://github.com/python/cpython/blob/main/Doc/library/glob.rst"
  306. rel="nofollow">Show Source
  307. </a>
  308. </li>
  309. </ul>
  310. </div>
  311. </div>
  312. <div id="sidebarbutton" title="Collapse sidebar">
  313. <span>«</span>
  314. </div>
  315. </div>
  316. <div class="clearer"></div>
  317. </div>
  318. <div class="related" role="navigation" aria-label="related navigation">
  319. <h3>Navigation</h3>
  320. <ul>
  321. <li class="right" style="margin-right: 10px">
  322. <a href="../genindex.html" title="General Index"
  323. >index</a></li>
  324. <li class="right" >
  325. <a href="../py-modindex.html" title="Python Module Index"
  326. >modules</a> |</li>
  327. <li class="right" >
  328. <a href="fnmatch.html" title="fnmatch — Unix filename pattern matching"
  329. >next</a> |</li>
  330. <li class="right" >
  331. <a href="tempfile.html" title="tempfile — Generate temporary files and directories"
  332. >previous</a> |</li>
  333. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  334. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  335. <li class="switchers">
  336. <div class="language_switcher_placeholder"></div>
  337. <div class="version_switcher_placeholder"></div>
  338. </li>
  339. <li>
  340. </li>
  341. <li id="cpython-language-and-version">
  342. <a href="../index.html">3.12.3 Documentation</a> &#187;
  343. </li>
  344. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  345. <li class="nav-item nav-item-2"><a href="filesys.html" >File and Directory Access</a> &#187;</li>
  346. <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>
  347. <li class="right">
  348. <div class="inline-search" role="search">
  349. <form class="inline-search" action="../search.html" method="get">
  350. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  351. <input type="submit" value="Go" />
  352. </form>
  353. </div>
  354. |
  355. </li>
  356. <li class="right">
  357. <label class="theme-selector-label">
  358. Theme
  359. <select class="theme-selector" oninput="activateTheme(this.value)">
  360. <option value="auto" selected>Auto</option>
  361. <option value="light">Light</option>
  362. <option value="dark">Dark</option>
  363. </select>
  364. </label> |</li>
  365. </ul>
  366. </div>
  367. <div class="footer">
  368. &copy;
  369. <a href="../copyright.html">
  370. Copyright
  371. </a>
  372. 2001-2024, Python Software Foundation.
  373. <br />
  374. This page is licensed under the Python Software Foundation License Version 2.
  375. <br />
  376. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  377. <br />
  378. See <a href="/license.html">History and License</a> for more information.<br />
  379. <br />
  380. The Python Software Foundation is a non-profit corporation.
  381. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  382. <br />
  383. <br />
  384. Last updated on Apr 09, 2024 (13:47 UTC).
  385. <a href="/bugs.html">Found a bug</a>?
  386. <br />
  387. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  388. </div>
  389. </body>
  390. </html>
上海开阖软件有限公司 沪ICP备12045867号-1