gooderp18绿色标准版
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

574 lines
42KB

  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="importlib.resources – Package resource reading, opening and access" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/importlib.resources.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/importlib/resources/__init__.py This module leverages Python’s import system to provide access to resources within packages. “Resources” are file-like resources associated with a m..." />
  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/importlib/resources/__init__.py This module leverages Python’s import system to provide access to resources within packages. “Resources” are file-like resources associated with a m..." />
  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>importlib.resources – Package resource reading, opening and access &#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="importlib.resources.abc – Abstract base classes for resources" href="importlib.resources.abc.html" />
  33. <link rel="prev" title="importlib — The implementation of import" href="importlib.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/importlib.resources.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. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib.resources</span></code> – Package resource reading, opening and access</a><ul>
  86. <li><a class="reference internal" href="#deprecated-functions">Deprecated functions</a></li>
  87. </ul>
  88. </li>
  89. </ul>
  90. </div>
  91. <div>
  92. <h4>Previous topic</h4>
  93. <p class="topless"><a href="importlib.html"
  94. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib</span></code> — The implementation of <code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a></p>
  95. </div>
  96. <div>
  97. <h4>Next topic</h4>
  98. <p class="topless"><a href="importlib.resources.abc.html"
  99. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib.resources.abc</span></code> – Abstract base classes for resources</a></p>
  100. </div>
  101. <div role="note" aria-label="source link">
  102. <h3>This Page</h3>
  103. <ul class="this-page-menu">
  104. <li><a href="../bugs.html">Report a Bug</a></li>
  105. <li>
  106. <a href="https://github.com/python/cpython/blob/main/Doc/library/importlib.resources.rst"
  107. rel="nofollow">Show Source
  108. </a>
  109. </li>
  110. </ul>
  111. </div>
  112. </nav>
  113. </div>
  114. </div>
  115. <div class="related" role="navigation" aria-label="related navigation">
  116. <h3>Navigation</h3>
  117. <ul>
  118. <li class="right" style="margin-right: 10px">
  119. <a href="../genindex.html" title="General Index"
  120. accesskey="I">index</a></li>
  121. <li class="right" >
  122. <a href="../py-modindex.html" title="Python Module Index"
  123. >modules</a> |</li>
  124. <li class="right" >
  125. <a href="importlib.resources.abc.html" title="importlib.resources.abc – Abstract base classes for resources"
  126. accesskey="N">next</a> |</li>
  127. <li class="right" >
  128. <a href="importlib.html" title="importlib — The implementation of import"
  129. accesskey="P">previous</a> |</li>
  130. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  131. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  132. <li class="switchers">
  133. <div class="language_switcher_placeholder"></div>
  134. <div class="version_switcher_placeholder"></div>
  135. </li>
  136. <li>
  137. </li>
  138. <li id="cpython-language-and-version">
  139. <a href="../index.html">3.12.3 Documentation</a> &#187;
  140. </li>
  141. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  142. <li class="nav-item nav-item-2"><a href="modules.html" accesskey="U">Importing Modules</a> &#187;</li>
  143. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib.resources</span></code> – Package resource reading, opening and access</a></li>
  144. <li class="right">
  145. <div class="inline-search" role="search">
  146. <form class="inline-search" action="../search.html" method="get">
  147. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  148. <input type="submit" value="Go" />
  149. </form>
  150. </div>
  151. |
  152. </li>
  153. <li class="right">
  154. <label class="theme-selector-label">
  155. Theme
  156. <select class="theme-selector" oninput="activateTheme(this.value)">
  157. <option value="auto" selected>Auto</option>
  158. <option value="light">Light</option>
  159. <option value="dark">Dark</option>
  160. </select>
  161. </label> |</li>
  162. </ul>
  163. </div>
  164. <div class="document">
  165. <div class="documentwrapper">
  166. <div class="bodywrapper">
  167. <div class="body" role="main">
  168. <section id="module-importlib.resources">
  169. <span id="importlib-resources-package-resource-reading-opening-and-access"></span><h1><a class="reference internal" href="#module-importlib.resources" title="importlib.resources: Package resource reading, opening, and access"><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib.resources</span></code></a> – Package resource reading, opening and access<a class="headerlink" href="#module-importlib.resources" title="Link to this heading">¶</a></h1>
  170. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/importlib/resources/__init__.py">Lib/importlib/resources/__init__.py</a></p>
  171. <hr class="docutils" />
  172. <div class="versionadded">
  173. <p><span class="versionmodified added">New in version 3.7.</span></p>
  174. </div>
  175. <p>This module leverages Python’s import system to provide access to <em>resources</em>
  176. within <em>packages</em>.</p>
  177. <p>“Resources” are file-like resources associated with a module or package in
  178. Python. The resources may be contained directly in a package, within a
  179. subdirectory contained in that package, or adjacent to modules outside a
  180. package. Resources may be text or binary. As a result, Python module sources
  181. (.py) of a package and compilation artifacts (pycache) are technically
  182. de-facto resources of that package. In practice, however, resources are
  183. primarily those non-Python artifacts exposed specifically by the package
  184. author.</p>
  185. <p>Resources can be opened or read in either binary or text mode.</p>
  186. <p>Resources are roughly akin to files inside directories, though it’s important
  187. to keep in mind that this is just a metaphor. Resources and packages <strong>do
  188. not</strong> have to exist as physical files and directories on the file system:
  189. for example, a package and its resources can be imported from a zip file using
  190. <a class="reference internal" href="zipimport.html#module-zipimport" title="zipimport: Support for importing Python modules from ZIP archives."><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipimport</span></code></a>.</p>
  191. <div class="admonition note">
  192. <p class="admonition-title">Note</p>
  193. <p>This module provides functionality similar to <a class="reference external" href="https://setuptools.readthedocs.io/en/latest/pkg_resources.html">pkg_resources</a> <a class="reference external" href="https://setuptools.readthedocs.io/en/latest/pkg_resources.html#basic-resource-access">Basic
  194. Resource Access</a>
  195. without the performance overhead of that package. This makes reading
  196. resources included in packages easier, with more stable and consistent
  197. semantics.</p>
  198. <p>The standalone backport of this module provides more information
  199. on <a class="reference external" href="https://importlib-resources.readthedocs.io/en/latest/using.html">using importlib.resources</a> and
  200. <a class="reference external" href="https://importlib-resources.readthedocs.io/en/latest/migration.html">migrating from pkg_resources to importlib.resources</a>.</p>
  201. </div>
  202. <p><a class="reference internal" href="importlib.html#importlib.abc.Loader" title="importlib.abc.Loader"><code class="xref py py-class docutils literal notranslate"><span class="pre">Loaders</span></code></a> that wish to support resource reading should implement a
  203. <code class="docutils literal notranslate"><span class="pre">get_resource_reader(fullname)</span></code> method as specified by
  204. <a class="reference internal" href="importlib.resources.abc.html#importlib.resources.abc.ResourceReader" title="importlib.resources.abc.ResourceReader"><code class="xref py py-class docutils literal notranslate"><span class="pre">importlib.resources.abc.ResourceReader</span></code></a>.</p>
  205. <dl class="py class">
  206. <dt class="sig sig-object py" id="importlib.resources.Anchor">
  207. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">importlib.resources.</span></span><span class="sig-name descname"><span class="pre">Anchor</span></span><a class="headerlink" href="#importlib.resources.Anchor" title="Link to this definition">¶</a></dt>
  208. <dd><p>Represents an anchor for resources, either a <a class="reference internal" href="types.html#types.ModuleType" title="types.ModuleType"><code class="xref py py-class docutils literal notranslate"><span class="pre">module</span> <span class="pre">object</span></code></a> or a module name as a string. Defined as
  209. <code class="docutils literal notranslate"><span class="pre">Union[str,</span> <span class="pre">ModuleType]</span></code>.</p>
  210. </dd></dl>
  211. <dl class="py function">
  212. <dt class="sig sig-object py" id="importlib.resources.files">
  213. <span class="sig-prename descclassname"><span class="pre">importlib.resources.</span></span><span class="sig-name descname"><span class="pre">files</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">anchor</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference internal" href="#importlib.resources.Anchor" title="importlib.resources.Anchor"><span class="pre">Anchor</span></a><span class="w"> </span><span class="p"><span class="pre">|</span></span><span class="w"> </span><a class="reference internal" href="constants.html#None" title="None"><span class="pre">None</span></a></span><span class="w"> </span><span class="o"><span class="pre">=</span></span><span class="w"> </span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.resources.files" title="Link to this definition">¶</a></dt>
  214. <dd><p>Returns a <a class="reference internal" href="importlib.resources.abc.html#importlib.resources.abc.Traversable" title="importlib.resources.abc.Traversable"><code class="xref py py-class docutils literal notranslate"><span class="pre">Traversable</span></code></a> object
  215. representing the resource container (think directory) and its resources
  216. (think files). A Traversable may contain other containers (think
  217. subdirectories).</p>
  218. <p><em>anchor</em> is an optional <a class="reference internal" href="#importlib.resources.Anchor" title="importlib.resources.Anchor"><code class="xref py py-class docutils literal notranslate"><span class="pre">Anchor</span></code></a>. If the anchor is a
  219. package, resources are resolved from that package. If a module,
  220. resources are resolved adjacent to that module (in the same package
  221. or the package root). If the anchor is omitted, the caller’s module
  222. is used.</p>
  223. <div class="versionadded">
  224. <p><span class="versionmodified added">New in version 3.9.</span></p>
  225. </div>
  226. <div class="versionchanged">
  227. <p><span class="versionmodified changed">Changed in version 3.12: </span><em>package</em> parameter was renamed to <em>anchor</em>. <em>anchor</em> can now
  228. be a non-package module and if omitted will default to the caller’s
  229. module. <em>package</em> is still accepted for compatibility but will raise
  230. a <a class="reference internal" href="exceptions.html#DeprecationWarning" title="DeprecationWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">DeprecationWarning</span></code></a>. Consider passing the anchor positionally or
  231. using <code class="docutils literal notranslate"><span class="pre">importlib_resources</span> <span class="pre">&gt;=</span> <span class="pre">5.10</span></code> for a compatible interface
  232. on older Pythons.</p>
  233. </div>
  234. </dd></dl>
  235. <dl class="py function">
  236. <dt class="sig sig-object py" id="importlib.resources.as_file">
  237. <span class="sig-prename descclassname"><span class="pre">importlib.resources.</span></span><span class="sig-name descname"><span class="pre">as_file</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">traversable</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.resources.as_file" title="Link to this definition">¶</a></dt>
  238. <dd><p>Given a <a class="reference internal" href="importlib.resources.abc.html#importlib.resources.abc.Traversable" title="importlib.resources.abc.Traversable"><code class="xref py py-class docutils literal notranslate"><span class="pre">Traversable</span></code></a> object representing
  239. a file or directory, typically from <a class="reference internal" href="#importlib.resources.files" title="importlib.resources.files"><code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.resources.files()</span></code></a>,
  240. return a context manager for use in a <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> statement.
  241. The context manager provides a <a class="reference internal" href="pathlib.html#pathlib.Path" title="pathlib.Path"><code class="xref py py-class docutils literal notranslate"><span class="pre">pathlib.Path</span></code></a> object.</p>
  242. <p>Exiting the context manager cleans up any temporary file or directory
  243. created when the resource was extracted from e.g. a zip file.</p>
  244. <p>Use <code class="docutils literal notranslate"><span class="pre">as_file</span></code> when the Traversable methods
  245. (<code class="docutils literal notranslate"><span class="pre">read_text</span></code>, etc) are insufficient and an actual file or directory on
  246. the file system is required.</p>
  247. <div class="versionadded">
  248. <p><span class="versionmodified added">New in version 3.9.</span></p>
  249. </div>
  250. <div class="versionchanged">
  251. <p><span class="versionmodified changed">Changed in version 3.12: </span>Added support for <em>traversable</em> representing a directory.</p>
  252. </div>
  253. </dd></dl>
  254. <section id="deprecated-functions">
  255. <h2>Deprecated functions<a class="headerlink" href="#deprecated-functions" title="Link to this heading">¶</a></h2>
  256. <p>An older, deprecated set of functions is still available, but is
  257. scheduled for removal in a future version of Python.
  258. The main drawback of these functions is that they do not support
  259. directories: they assume all resources are located directly within a <em>package</em>.</p>
  260. <dl class="py data">
  261. <dt class="sig sig-object py" id="importlib.resources.Package">
  262. <span class="sig-prename descclassname"><span class="pre">importlib.resources.</span></span><span class="sig-name descname"><span class="pre">Package</span></span><a class="headerlink" href="#importlib.resources.Package" title="Link to this definition">¶</a></dt>
  263. <dd><blockquote>
  264. <div><p>Whenever a function accepts a <code class="docutils literal notranslate"><span class="pre">Package</span></code> argument, you can pass in
  265. either a <a class="reference internal" href="types.html#types.ModuleType" title="types.ModuleType"><code class="xref py py-class docutils literal notranslate"><span class="pre">module</span> <span class="pre">object</span></code></a> or a module name
  266. as a string. You can only pass module objects whose
  267. <code class="docutils literal notranslate"><span class="pre">__spec__.submodule_search_locations</span></code> is not <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  268. <p>The <code class="docutils literal notranslate"><span class="pre">Package</span></code> type is defined as <code class="docutils literal notranslate"><span class="pre">Union[str,</span> <span class="pre">ModuleType]</span></code>.</p>
  269. </div></blockquote>
  270. <div class="deprecated">
  271. <p><span class="versionmodified deprecated">Deprecated since version 3.12.</span></p>
  272. </div>
  273. </dd></dl>
  274. <dl class="py data">
  275. <dt class="sig sig-object py" id="importlib.resources.Resource">
  276. <span class="sig-prename descclassname"><span class="pre">importlib.resources.</span></span><span class="sig-name descname"><span class="pre">Resource</span></span><a class="headerlink" href="#importlib.resources.Resource" title="Link to this definition">¶</a></dt>
  277. <dd><p>For <em>resource</em> arguments of the functions below, you can pass in
  278. the name of a resource as a string or
  279. a <a class="reference internal" href="os.html#os.PathLike" title="os.PathLike"><code class="xref py py-class docutils literal notranslate"><span class="pre">path-like</span> <span class="pre">object</span></code></a>.</p>
  280. <p>The <code class="docutils literal notranslate"><span class="pre">Resource</span></code> type is defined as <code class="docutils literal notranslate"><span class="pre">Union[str,</span> <span class="pre">os.PathLike]</span></code>.</p>
  281. </dd></dl>
  282. <dl class="py function">
  283. <dt class="sig sig-object py" id="importlib.resources.open_binary">
  284. <span class="sig-prename descclassname"><span class="pre">importlib.resources.</span></span><span class="sig-name descname"><span class="pre">open_binary</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">package</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">resource</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.resources.open_binary" title="Link to this definition">¶</a></dt>
  285. <dd><p>Open for binary reading the <em>resource</em> within <em>package</em>.</p>
  286. <p><em>package</em> is either a name or a module object which conforms to the
  287. <code class="docutils literal notranslate"><span class="pre">Package</span></code> requirements. <em>resource</em> is the name of the resource to open
  288. within <em>package</em>; it may not contain path separators and it may not have
  289. sub-resources (i.e. it cannot be a directory). This function returns a
  290. <code class="docutils literal notranslate"><span class="pre">typing.BinaryIO</span></code> instance, a binary I/O stream open for reading.</p>
  291. <div class="deprecated">
  292. <p><span class="versionmodified deprecated">Deprecated since version 3.11: </span>Calls to this function can be replaced by:</p>
  293. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">files</span><span class="p">(</span><span class="n">package</span><span class="p">)</span><span class="o">.</span><span class="n">joinpath</span><span class="p">(</span><span class="n">resource</span><span class="p">)</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s1">&#39;rb&#39;</span><span class="p">)</span>
  294. </pre></div>
  295. </div>
  296. </div>
  297. </dd></dl>
  298. <dl class="py function">
  299. <dt class="sig sig-object py" id="importlib.resources.open_text">
  300. <span class="sig-prename descclassname"><span class="pre">importlib.resources.</span></span><span class="sig-name descname"><span class="pre">open_text</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">package</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">resource</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">encoding</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'utf-8'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">errors</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'strict'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.resources.open_text" title="Link to this definition">¶</a></dt>
  301. <dd><p>Open for text reading the <em>resource</em> within <em>package</em>. By default, the
  302. resource is opened for reading as UTF-8.</p>
  303. <p><em>package</em> is either a name or a module object which conforms to the
  304. <code class="docutils literal notranslate"><span class="pre">Package</span></code> requirements. <em>resource</em> is the name of the resource to open
  305. within <em>package</em>; it may not contain path separators and it may not have
  306. sub-resources (i.e. it cannot be a directory). <em>encoding</em> and <em>errors</em>
  307. have the same meaning as with built-in <a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a>.</p>
  308. <p>This function returns a <code class="docutils literal notranslate"><span class="pre">typing.TextIO</span></code> instance, a text I/O stream open
  309. for reading.</p>
  310. <div class="deprecated">
  311. <p><span class="versionmodified deprecated">Deprecated since version 3.11: </span>Calls to this function can be replaced by:</p>
  312. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">files</span><span class="p">(</span><span class="n">package</span><span class="p">)</span><span class="o">.</span><span class="n">joinpath</span><span class="p">(</span><span class="n">resource</span><span class="p">)</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s1">&#39;r&#39;</span><span class="p">,</span> <span class="n">encoding</span><span class="o">=</span><span class="n">encoding</span><span class="p">)</span>
  313. </pre></div>
  314. </div>
  315. </div>
  316. </dd></dl>
  317. <dl class="py function">
  318. <dt class="sig sig-object py" id="importlib.resources.read_binary">
  319. <span class="sig-prename descclassname"><span class="pre">importlib.resources.</span></span><span class="sig-name descname"><span class="pre">read_binary</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">package</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">resource</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.resources.read_binary" title="Link to this definition">¶</a></dt>
  320. <dd><p>Read and return the contents of the <em>resource</em> within <em>package</em> as
  321. <code class="docutils literal notranslate"><span class="pre">bytes</span></code>.</p>
  322. <p><em>package</em> is either a name or a module object which conforms to the
  323. <code class="docutils literal notranslate"><span class="pre">Package</span></code> requirements. <em>resource</em> is the name of the resource to open
  324. within <em>package</em>; it may not contain path separators and it may not have
  325. sub-resources (i.e. it cannot be a directory). This function returns the
  326. contents of the resource as <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>.</p>
  327. <div class="deprecated">
  328. <p><span class="versionmodified deprecated">Deprecated since version 3.11: </span>Calls to this function can be replaced by:</p>
  329. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">files</span><span class="p">(</span><span class="n">package</span><span class="p">)</span><span class="o">.</span><span class="n">joinpath</span><span class="p">(</span><span class="n">resource</span><span class="p">)</span><span class="o">.</span><span class="n">read_bytes</span><span class="p">()</span>
  330. </pre></div>
  331. </div>
  332. </div>
  333. </dd></dl>
  334. <dl class="py function">
  335. <dt class="sig sig-object py" id="importlib.resources.read_text">
  336. <span class="sig-prename descclassname"><span class="pre">importlib.resources.</span></span><span class="sig-name descname"><span class="pre">read_text</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">package</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">resource</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">encoding</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'utf-8'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">errors</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'strict'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.resources.read_text" title="Link to this definition">¶</a></dt>
  337. <dd><p>Read and return the contents of <em>resource</em> within <em>package</em> as a <code class="docutils literal notranslate"><span class="pre">str</span></code>.
  338. By default, the contents are read as strict UTF-8.</p>
  339. <p><em>package</em> is either a name or a module object which conforms to the
  340. <code class="docutils literal notranslate"><span class="pre">Package</span></code> requirements. <em>resource</em> is the name of the resource to open
  341. within <em>package</em>; it may not contain path separators and it may not have
  342. sub-resources (i.e. it cannot be a directory). <em>encoding</em> and <em>errors</em>
  343. have the same meaning as with built-in <a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a>. This function
  344. returns the contents of the resource as <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a>.</p>
  345. <div class="deprecated">
  346. <p><span class="versionmodified deprecated">Deprecated since version 3.11: </span>Calls to this function can be replaced by:</p>
  347. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">files</span><span class="p">(</span><span class="n">package</span><span class="p">)</span><span class="o">.</span><span class="n">joinpath</span><span class="p">(</span><span class="n">resource</span><span class="p">)</span><span class="o">.</span><span class="n">read_text</span><span class="p">(</span><span class="n">encoding</span><span class="o">=</span><span class="n">encoding</span><span class="p">)</span>
  348. </pre></div>
  349. </div>
  350. </div>
  351. </dd></dl>
  352. <dl class="py function">
  353. <dt class="sig sig-object py" id="importlib.resources.path">
  354. <span class="sig-prename descclassname"><span class="pre">importlib.resources.</span></span><span class="sig-name descname"><span class="pre">path</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">package</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">resource</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.resources.path" title="Link to this definition">¶</a></dt>
  355. <dd><p>Return the path to the <em>resource</em> as an actual file system path. This
  356. function returns a context manager for use in a <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> statement.
  357. The context manager provides a <a class="reference internal" href="pathlib.html#pathlib.Path" title="pathlib.Path"><code class="xref py py-class docutils literal notranslate"><span class="pre">pathlib.Path</span></code></a> object.</p>
  358. <p>Exiting the context manager cleans up any temporary file created when the
  359. resource needs to be extracted from e.g. a zip file.</p>
  360. <p><em>package</em> is either a name or a module object which conforms to the
  361. <code class="docutils literal notranslate"><span class="pre">Package</span></code> requirements. <em>resource</em> is the name of the resource to open
  362. within <em>package</em>; it may not contain path separators and it may not have
  363. sub-resources (i.e. it cannot be a directory).</p>
  364. <div class="deprecated">
  365. <p><span class="versionmodified deprecated">Deprecated since version 3.11: </span>Calls to this function can be replaced using <a class="reference internal" href="#importlib.resources.as_file" title="importlib.resources.as_file"><code class="xref py py-func docutils literal notranslate"><span class="pre">as_file()</span></code></a>:</p>
  366. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">as_file</span><span class="p">(</span><span class="n">files</span><span class="p">(</span><span class="n">package</span><span class="p">)</span><span class="o">.</span><span class="n">joinpath</span><span class="p">(</span><span class="n">resource</span><span class="p">))</span>
  367. </pre></div>
  368. </div>
  369. </div>
  370. </dd></dl>
  371. <dl class="py function">
  372. <dt class="sig sig-object py" id="importlib.resources.is_resource">
  373. <span class="sig-prename descclassname"><span class="pre">importlib.resources.</span></span><span class="sig-name descname"><span class="pre">is_resource</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">package</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">name</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.resources.is_resource" title="Link to this definition">¶</a></dt>
  374. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if there is a resource named <em>name</em> in the package,
  375. otherwise <code class="docutils literal notranslate"><span class="pre">False</span></code>.
  376. This function does not consider directories to be resources.
  377. <em>package</em> is either a name or a module object which conforms to the
  378. <code class="docutils literal notranslate"><span class="pre">Package</span></code> requirements.</p>
  379. <div class="deprecated">
  380. <p><span class="versionmodified deprecated">Deprecated since version 3.11: </span>Calls to this function can be replaced by:</p>
  381. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">files</span><span class="p">(</span><span class="n">package</span><span class="p">)</span><span class="o">.</span><span class="n">joinpath</span><span class="p">(</span><span class="n">resource</span><span class="p">)</span><span class="o">.</span><span class="n">is_file</span><span class="p">()</span>
  382. </pre></div>
  383. </div>
  384. </div>
  385. </dd></dl>
  386. <dl class="py function">
  387. <dt class="sig sig-object py" id="importlib.resources.contents">
  388. <span class="sig-prename descclassname"><span class="pre">importlib.resources.</span></span><span class="sig-name descname"><span class="pre">contents</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">package</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#importlib.resources.contents" title="Link to this definition">¶</a></dt>
  389. <dd><p>Return an iterable over the named items within the package. The iterable
  390. returns <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> resources (e.g. files) and non-resources
  391. (e.g. directories). The iterable does not recurse into subdirectories.</p>
  392. <p><em>package</em> is either a name or a module object which conforms to the
  393. <code class="docutils literal notranslate"><span class="pre">Package</span></code> requirements.</p>
  394. <div class="deprecated">
  395. <p><span class="versionmodified deprecated">Deprecated since version 3.11: </span>Calls to this function can be replaced by:</p>
  396. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="p">(</span><span class="n">resource</span><span class="o">.</span><span class="n">name</span> <span class="k">for</span> <span class="n">resource</span> <span class="ow">in</span> <span class="n">files</span><span class="p">(</span><span class="n">package</span><span class="p">)</span><span class="o">.</span><span class="n">iterdir</span><span class="p">()</span> <span class="k">if</span> <span class="n">resource</span><span class="o">.</span><span class="n">is_file</span><span class="p">())</span>
  397. </pre></div>
  398. </div>
  399. </div>
  400. </dd></dl>
  401. </section>
  402. </section>
  403. <div class="clearer"></div>
  404. </div>
  405. </div>
  406. </div>
  407. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  408. <div class="sphinxsidebarwrapper">
  409. <div>
  410. <h3><a href="../contents.html">Table of Contents</a></h3>
  411. <ul>
  412. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib.resources</span></code> – Package resource reading, opening and access</a><ul>
  413. <li><a class="reference internal" href="#deprecated-functions">Deprecated functions</a></li>
  414. </ul>
  415. </li>
  416. </ul>
  417. </div>
  418. <div>
  419. <h4>Previous topic</h4>
  420. <p class="topless"><a href="importlib.html"
  421. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib</span></code> — The implementation of <code class="xref std std-keyword docutils literal notranslate"><span class="pre">import</span></code></a></p>
  422. </div>
  423. <div>
  424. <h4>Next topic</h4>
  425. <p class="topless"><a href="importlib.resources.abc.html"
  426. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib.resources.abc</span></code> – Abstract base classes for resources</a></p>
  427. </div>
  428. <div role="note" aria-label="source link">
  429. <h3>This Page</h3>
  430. <ul class="this-page-menu">
  431. <li><a href="../bugs.html">Report a Bug</a></li>
  432. <li>
  433. <a href="https://github.com/python/cpython/blob/main/Doc/library/importlib.resources.rst"
  434. rel="nofollow">Show Source
  435. </a>
  436. </li>
  437. </ul>
  438. </div>
  439. </div>
  440. <div id="sidebarbutton" title="Collapse sidebar">
  441. <span>«</span>
  442. </div>
  443. </div>
  444. <div class="clearer"></div>
  445. </div>
  446. <div class="related" role="navigation" aria-label="related navigation">
  447. <h3>Navigation</h3>
  448. <ul>
  449. <li class="right" style="margin-right: 10px">
  450. <a href="../genindex.html" title="General Index"
  451. >index</a></li>
  452. <li class="right" >
  453. <a href="../py-modindex.html" title="Python Module Index"
  454. >modules</a> |</li>
  455. <li class="right" >
  456. <a href="importlib.resources.abc.html" title="importlib.resources.abc – Abstract base classes for resources"
  457. >next</a> |</li>
  458. <li class="right" >
  459. <a href="importlib.html" title="importlib — The implementation of import"
  460. >previous</a> |</li>
  461. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  462. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  463. <li class="switchers">
  464. <div class="language_switcher_placeholder"></div>
  465. <div class="version_switcher_placeholder"></div>
  466. </li>
  467. <li>
  468. </li>
  469. <li id="cpython-language-and-version">
  470. <a href="../index.html">3.12.3 Documentation</a> &#187;
  471. </li>
  472. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  473. <li class="nav-item nav-item-2"><a href="modules.html" >Importing Modules</a> &#187;</li>
  474. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib.resources</span></code> – Package resource reading, opening and access</a></li>
  475. <li class="right">
  476. <div class="inline-search" role="search">
  477. <form class="inline-search" action="../search.html" method="get">
  478. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  479. <input type="submit" value="Go" />
  480. </form>
  481. </div>
  482. |
  483. </li>
  484. <li class="right">
  485. <label class="theme-selector-label">
  486. Theme
  487. <select class="theme-selector" oninput="activateTheme(this.value)">
  488. <option value="auto" selected>Auto</option>
  489. <option value="light">Light</option>
  490. <option value="dark">Dark</option>
  491. </select>
  492. </label> |</li>
  493. </ul>
  494. </div>
  495. <div class="footer">
  496. &copy;
  497. <a href="../copyright.html">
  498. Copyright
  499. </a>
  500. 2001-2024, Python Software Foundation.
  501. <br />
  502. This page is licensed under the Python Software Foundation License Version 2.
  503. <br />
  504. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  505. <br />
  506. See <a href="/license.html">History and License</a> for more information.<br />
  507. <br />
  508. The Python Software Foundation is a non-profit corporation.
  509. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  510. <br />
  511. <br />
  512. Last updated on Apr 09, 2024 (13:47 UTC).
  513. <a href="/bugs.html">Found a bug</a>?
  514. <br />
  515. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  516. </div>
  517. </body>
  518. </html>
上海开阖软件有限公司 沪ICP备12045867号-1