gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

776 line
70KB

  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="tempfile — Generate temporary files and directories" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/tempfile.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/tempfile.py This module creates temporary files and directories. It works on all supported platforms. TemporaryFile, NamedTemporaryFile, TemporaryDirectory, and SpooledTemporaryFil..." />
  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/tempfile.py This module creates temporary files and directories. It works on all supported platforms. TemporaryFile, NamedTemporaryFile, TemporaryDirectory, and SpooledTemporaryFil..." />
  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>tempfile — Generate temporary files and directories &#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="glob — Unix style pathname pattern expansion" href="glob.html" />
  33. <link rel="prev" title="filecmp — File and Directory Comparisons" href="filecmp.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/tempfile.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">tempfile</span></code> — Generate temporary files and directories</a><ul>
  86. <li><a class="reference internal" href="#examples">Examples</a></li>
  87. <li><a class="reference internal" href="#deprecated-functions-and-variables">Deprecated functions and variables</a></li>
  88. </ul>
  89. </li>
  90. </ul>
  91. </div>
  92. <div>
  93. <h4>Previous topic</h4>
  94. <p class="topless"><a href="filecmp.html"
  95. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">filecmp</span></code> — File and Directory Comparisons</a></p>
  96. </div>
  97. <div>
  98. <h4>Next topic</h4>
  99. <p class="topless"><a href="glob.html"
  100. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">glob</span></code> — Unix style pathname pattern expansion</a></p>
  101. </div>
  102. <div role="note" aria-label="source link">
  103. <h3>This Page</h3>
  104. <ul class="this-page-menu">
  105. <li><a href="../bugs.html">Report a Bug</a></li>
  106. <li>
  107. <a href="https://github.com/python/cpython/blob/main/Doc/library/tempfile.rst"
  108. rel="nofollow">Show Source
  109. </a>
  110. </li>
  111. </ul>
  112. </div>
  113. </nav>
  114. </div>
  115. </div>
  116. <div class="related" role="navigation" aria-label="related navigation">
  117. <h3>Navigation</h3>
  118. <ul>
  119. <li class="right" style="margin-right: 10px">
  120. <a href="../genindex.html" title="General Index"
  121. accesskey="I">index</a></li>
  122. <li class="right" >
  123. <a href="../py-modindex.html" title="Python Module Index"
  124. >modules</a> |</li>
  125. <li class="right" >
  126. <a href="glob.html" title="glob — Unix style pathname pattern expansion"
  127. accesskey="N">next</a> |</li>
  128. <li class="right" >
  129. <a href="filecmp.html" title="filecmp — File and Directory Comparisons"
  130. accesskey="P">previous</a> |</li>
  131. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  132. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  133. <li class="switchers">
  134. <div class="language_switcher_placeholder"></div>
  135. <div class="version_switcher_placeholder"></div>
  136. </li>
  137. <li>
  138. </li>
  139. <li id="cpython-language-and-version">
  140. <a href="../index.html">3.12.3 Documentation</a> &#187;
  141. </li>
  142. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  143. <li class="nav-item nav-item-2"><a href="filesys.html" accesskey="U">File and Directory Access</a> &#187;</li>
  144. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">tempfile</span></code> — Generate temporary files and directories</a></li>
  145. <li class="right">
  146. <div class="inline-search" role="search">
  147. <form class="inline-search" action="../search.html" method="get">
  148. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  149. <input type="submit" value="Go" />
  150. </form>
  151. </div>
  152. |
  153. </li>
  154. <li class="right">
  155. <label class="theme-selector-label">
  156. Theme
  157. <select class="theme-selector" oninput="activateTheme(this.value)">
  158. <option value="auto" selected>Auto</option>
  159. <option value="light">Light</option>
  160. <option value="dark">Dark</option>
  161. </select>
  162. </label> |</li>
  163. </ul>
  164. </div>
  165. <div class="document">
  166. <div class="documentwrapper">
  167. <div class="bodywrapper">
  168. <div class="body" role="main">
  169. <section id="module-tempfile">
  170. <span id="tempfile-generate-temporary-files-and-directories"></span><h1><a class="reference internal" href="#module-tempfile" title="tempfile: Generate temporary files and directories."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tempfile</span></code></a> — Generate temporary files and directories<a class="headerlink" href="#module-tempfile" title="Link to this heading">¶</a></h1>
  171. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/tempfile.py">Lib/tempfile.py</a></p>
  172. <hr class="docutils" id="index-0" />
  173. <p>This module creates temporary files and directories. It works on all
  174. supported platforms. <a class="reference internal" href="#tempfile.TemporaryFile" title="tempfile.TemporaryFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">TemporaryFile</span></code></a>, <a class="reference internal" href="#tempfile.NamedTemporaryFile" title="tempfile.NamedTemporaryFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">NamedTemporaryFile</span></code></a>,
  175. <a class="reference internal" href="#tempfile.TemporaryDirectory" title="tempfile.TemporaryDirectory"><code class="xref py py-class docutils literal notranslate"><span class="pre">TemporaryDirectory</span></code></a>, and <a class="reference internal" href="#tempfile.SpooledTemporaryFile" title="tempfile.SpooledTemporaryFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">SpooledTemporaryFile</span></code></a> are high-level
  176. interfaces which provide automatic cleanup and can be used as
  177. <a class="reference internal" href="../glossary.html#term-context-manager"><span class="xref std std-term">context managers</span></a>. <a class="reference internal" href="#tempfile.mkstemp" title="tempfile.mkstemp"><code class="xref py py-func docutils literal notranslate"><span class="pre">mkstemp()</span></code></a> and
  178. <a class="reference internal" href="#tempfile.mkdtemp" title="tempfile.mkdtemp"><code class="xref py py-func docutils literal notranslate"><span class="pre">mkdtemp()</span></code></a> are lower-level functions which require manual cleanup.</p>
  179. <p>All the user-callable functions and constructors take additional arguments which
  180. allow direct control over the location and name of temporary files and
  181. directories. Files names used by this module include a string of
  182. random characters which allows those files to be securely created in
  183. shared temporary directories.
  184. To maintain backward compatibility, the argument order is somewhat odd; it
  185. is recommended to use keyword arguments for clarity.</p>
  186. <p>The module defines the following user-callable items:</p>
  187. <dl class="py function">
  188. <dt class="sig sig-object py" id="tempfile.TemporaryFile">
  189. <span class="sig-prename descclassname"><span class="pre">tempfile.</span></span><span class="sig-name descname"><span class="pre">TemporaryFile</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">mode</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'w+b'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">buffering</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">-1</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">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">newline</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">suffix</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">prefix</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</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="o"><span class="pre">*</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">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#tempfile.TemporaryFile" title="Link to this definition">¶</a></dt>
  190. <dd><p>Return a <a class="reference internal" href="../glossary.html#term-file-like-object"><span class="xref std std-term">file-like object</span></a> that can be used as a temporary storage area.
  191. The file is created securely, using the same rules as <a class="reference internal" href="#tempfile.mkstemp" title="tempfile.mkstemp"><code class="xref py py-func docutils literal notranslate"><span class="pre">mkstemp()</span></code></a>. It will be destroyed as soon
  192. as it is closed (including an implicit close when the object is garbage
  193. collected). Under Unix, the directory entry for the file is either not created at all or is removed
  194. immediately after the file is created. Other platforms do not support
  195. this; your code should not rely on a temporary file created using this
  196. function having or not having a visible name in the file system.</p>
  197. <p>The resulting object can be used as a <a class="reference internal" href="../glossary.html#term-context-manager"><span class="xref std std-term">context manager</span></a> (see
  198. <a class="reference internal" href="#tempfile-examples"><span class="std std-ref">Examples</span></a>). On completion of the context or
  199. destruction of the file object the temporary file will be removed
  200. from the filesystem.</p>
  201. <p>The <em>mode</em> parameter defaults to <code class="docutils literal notranslate"><span class="pre">'w+b'</span></code> so that the file created can
  202. be read and written without being closed. Binary mode is used so that it
  203. behaves consistently on all platforms without regard for the data that is
  204. stored. <em>buffering</em>, <em>encoding</em>, <em>errors</em> and <em>newline</em> are interpreted as for
  205. <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>
  206. <p>The <em>dir</em>, <em>prefix</em> and <em>suffix</em> parameters have the same meaning and
  207. defaults as with <a class="reference internal" href="#tempfile.mkstemp" title="tempfile.mkstemp"><code class="xref py py-func docutils literal notranslate"><span class="pre">mkstemp()</span></code></a>.</p>
  208. <p>The returned object is a true file object on POSIX platforms. On other
  209. platforms, it is a file-like object whose <code class="xref py py-attr docutils literal notranslate"><span class="pre">file</span></code> attribute is the
  210. underlying true file object.</p>
  211. <p>The <a class="reference internal" href="os.html#os.O_TMPFILE" title="os.O_TMPFILE"><code class="xref py py-const docutils literal notranslate"><span class="pre">os.O_TMPFILE</span></code></a> flag is used if it is available and works
  212. (Linux-specific, requires Linux kernel 3.11 or later).</p>
  213. <p>On platforms that are neither Posix nor Cygwin, TemporaryFile is an alias
  214. for NamedTemporaryFile.</p>
  215. <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">tempfile.mkstemp</span></code> with argument <code class="docutils literal notranslate"><span class="pre">fullpath</span></code>.</p>
  216. <div class="versionchanged">
  217. <p><span class="versionmodified changed">Changed in version 3.5: </span>The <a class="reference internal" href="os.html#os.O_TMPFILE" title="os.O_TMPFILE"><code class="xref py py-const docutils literal notranslate"><span class="pre">os.O_TMPFILE</span></code></a> flag is now used if available.</p>
  218. </div>
  219. <div class="versionchanged">
  220. <p><span class="versionmodified changed">Changed in version 3.8: </span>Added <em>errors</em> parameter.</p>
  221. </div>
  222. </dd></dl>
  223. <dl class="py function">
  224. <dt class="sig sig-object py" id="tempfile.NamedTemporaryFile">
  225. <span class="sig-prename descclassname"><span class="pre">tempfile.</span></span><span class="sig-name descname"><span class="pre">NamedTemporaryFile</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">mode</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'w+b'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">buffering</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">-1</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">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">newline</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">suffix</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">prefix</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</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">delete</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</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">errors</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">delete_on_close</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#tempfile.NamedTemporaryFile" title="Link to this definition">¶</a></dt>
  226. <dd><p>This function operates exactly as <a class="reference internal" href="#tempfile.TemporaryFile" title="tempfile.TemporaryFile"><code class="xref py py-func docutils literal notranslate"><span class="pre">TemporaryFile()</span></code></a> does, except the
  227. following differences:</p>
  228. <ul class="simple">
  229. <li><p>This function returns a file that is guaranteed to have a visible name in
  230. the file system.</p></li>
  231. <li><p>To manage the named file, it extends the parameters of
  232. <a class="reference internal" href="#tempfile.TemporaryFile" title="tempfile.TemporaryFile"><code class="xref py py-func docutils literal notranslate"><span class="pre">TemporaryFile()</span></code></a> with <em>delete</em> and <em>delete_on_close</em> parameters that
  233. determine whether and how the named file should be automatically deleted.</p></li>
  234. </ul>
  235. <p>The returned object is always a <a class="reference internal" href="../glossary.html#term-file-like-object"><span class="xref std std-term">file-like object</span></a> whose <code class="xref py py-attr docutils literal notranslate"><span class="pre">file</span></code>
  236. attribute is the underlying true file object. This file-like object
  237. can be used 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, just like a normal file. The
  238. name of the temporary file can be retrieved from the <code class="xref py py-attr docutils literal notranslate"><span class="pre">name</span></code> attribute
  239. of the returned file-like object. On Unix, unlike with the
  240. <a class="reference internal" href="#tempfile.TemporaryFile" title="tempfile.TemporaryFile"><code class="xref py py-func docutils literal notranslate"><span class="pre">TemporaryFile()</span></code></a>, the directory entry does not get unlinked immediately
  241. after the file creation.</p>
  242. <p>If <em>delete</em> is true (the default) and <em>delete_on_close</em> is true (the
  243. default), the file is deleted as soon as it is closed. If <em>delete</em> is true
  244. and <em>delete_on_close</em> is false, the file is deleted on context manager exit
  245. only, or else when the <a class="reference internal" href="../glossary.html#term-file-like-object"><span class="xref std std-term">file-like object</span></a> is finalized. Deletion is not
  246. always guaranteed in this case (see <a class="reference internal" href="../reference/datamodel.html#object.__del__" title="object.__del__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">object.__del__()</span></code></a>). If <em>delete</em> is
  247. false, the value of <em>delete_on_close</em> is ignored.</p>
  248. <p>Therefore to use the name of the temporary file to reopen the file after
  249. closing it, either make sure not to delete the file upon closure (set the
  250. <em>delete</em> parameter to be false) or, in case the temporary file is created in
  251. 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, set the <em>delete_on_close</em> parameter to be false.
  252. The latter approach is recommended as it provides assistance in automatic
  253. cleaning of the temporary file upon the context manager exit.</p>
  254. <p>Opening the temporary file again by its name while it is still open works as
  255. follows:</p>
  256. <ul class="simple">
  257. <li><p>On POSIX the file can always be opened again.</p></li>
  258. <li><p>On Windows, make sure that at least one of the following conditions are
  259. fulfilled:</p>
  260. <ul>
  261. <li><p><em>delete</em> is false</p></li>
  262. <li><p>additional open shares delete access (e.g. by calling <a class="reference internal" href="os.html#os.open" title="os.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.open()</span></code></a>
  263. with the flag <code class="docutils literal notranslate"><span class="pre">O_TEMPORARY</span></code>)</p></li>
  264. <li><p><em>delete</em> is true but <em>delete_on_close</em> is false. Note, that in this
  265. case the additional opens that do not share delete access (e.g.
  266. created via builtin <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>) must be closed before exiting the
  267. context manager, else the <a class="reference internal" href="os.html#os.unlink" title="os.unlink"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.unlink()</span></code></a> call on context manager
  268. exit will fail with a <a class="reference internal" href="exceptions.html#PermissionError" title="PermissionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">PermissionError</span></code></a>.</p></li>
  269. </ul>
  270. </li>
  271. </ul>
  272. <p>On Windows, if <em>delete_on_close</em> is false, and the file is created in a
  273. directory for which the user lacks delete access, then the <a class="reference internal" href="os.html#os.unlink" title="os.unlink"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.unlink()</span></code></a>
  274. call on exit of the context manager will fail with a <a class="reference internal" href="exceptions.html#PermissionError" title="PermissionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">PermissionError</span></code></a>.
  275. This cannot happen when <em>delete_on_close</em> is true because delete access is
  276. requested by the open, which fails immediately if the requested access is not
  277. granted.</p>
  278. <p>On POSIX (only), a process that is terminated abruptly with SIGKILL
  279. cannot automatically delete any NamedTemporaryFiles it created.</p>
  280. <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">tempfile.mkstemp</span></code> with argument <code class="docutils literal notranslate"><span class="pre">fullpath</span></code>.</p>
  281. <div class="versionchanged">
  282. <p><span class="versionmodified changed">Changed in version 3.8: </span>Added <em>errors</em> parameter.</p>
  283. </div>
  284. <div class="versionchanged">
  285. <p><span class="versionmodified changed">Changed in version 3.12: </span>Added <em>delete_on_close</em> parameter.</p>
  286. </div>
  287. </dd></dl>
  288. <dl class="py class">
  289. <dt class="sig sig-object py" id="tempfile.SpooledTemporaryFile">
  290. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">tempfile.</span></span><span class="sig-name descname"><span class="pre">SpooledTemporaryFile</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">max_size</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">mode</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'w+b'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">buffering</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">-1</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">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">newline</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">suffix</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">prefix</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</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="o"><span class="pre">*</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">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#tempfile.SpooledTemporaryFile" title="Link to this definition">¶</a></dt>
  291. <dd><p>This class operates exactly as <a class="reference internal" href="#tempfile.TemporaryFile" title="tempfile.TemporaryFile"><code class="xref py py-func docutils literal notranslate"><span class="pre">TemporaryFile()</span></code></a> does, except that
  292. data is spooled in memory until the file size exceeds <em>max_size</em>, or
  293. until the file’s <a class="reference internal" href="io.html#io.IOBase.fileno" title="io.IOBase.fileno"><code class="xref py py-func docutils literal notranslate"><span class="pre">fileno()</span></code></a> method is called, at which point the
  294. contents are written to disk and operation proceeds as with
  295. <a class="reference internal" href="#tempfile.TemporaryFile" title="tempfile.TemporaryFile"><code class="xref py py-func docutils literal notranslate"><span class="pre">TemporaryFile()</span></code></a>.</p>
  296. <dl class="py method">
  297. <dt class="sig sig-object py" id="tempfile.SpooledTemporaryFile.rollover">
  298. <span class="sig-name descname"><span class="pre">rollover</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#tempfile.SpooledTemporaryFile.rollover" title="Link to this definition">¶</a></dt>
  299. <dd><p>The resulting file has one additional method, <code class="xref py py-meth docutils literal notranslate"><span class="pre">rollover()</span></code>, which
  300. causes the file to roll over to an on-disk file regardless of its size.</p>
  301. </dd></dl>
  302. <p>The returned object is a file-like object whose <code class="xref py py-attr docutils literal notranslate"><span class="pre">_file</span></code> attribute
  303. is either an <a class="reference internal" href="io.html#io.BytesIO" title="io.BytesIO"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.BytesIO</span></code></a> or <a class="reference internal" href="io.html#io.TextIOWrapper" title="io.TextIOWrapper"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.TextIOWrapper</span></code></a> object
  304. (depending on whether binary or text <em>mode</em> was specified) or a true file
  305. object, depending on whether <a class="reference internal" href="#tempfile.SpooledTemporaryFile.rollover" title="tempfile.SpooledTemporaryFile.rollover"><code class="xref py py-meth docutils literal notranslate"><span class="pre">rollover()</span></code></a> has been called. This
  306. file-like object can be used 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, just like
  307. a normal file.</p>
  308. <div class="versionchanged">
  309. <p><span class="versionmodified changed">Changed in version 3.3: </span>the truncate method now accepts a <em>size</em> argument.</p>
  310. </div>
  311. <div class="versionchanged">
  312. <p><span class="versionmodified changed">Changed in version 3.8: </span>Added <em>errors</em> parameter.</p>
  313. </div>
  314. <div class="versionchanged">
  315. <p><span class="versionmodified changed">Changed in version 3.11: </span>Fully implements the <a class="reference internal" href="io.html#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.BufferedIOBase</span></code></a> and
  316. <a class="reference internal" href="io.html#io.TextIOBase" title="io.TextIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.TextIOBase</span></code></a> abstract base classes (depending on whether binary
  317. or text <em>mode</em> was specified).</p>
  318. </div>
  319. </dd></dl>
  320. <dl class="py class">
  321. <dt class="sig sig-object py" id="tempfile.TemporaryDirectory">
  322. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">tempfile.</span></span><span class="sig-name descname"><span class="pre">TemporaryDirectory</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">suffix</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">prefix</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</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">ignore_cleanup_errors</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="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">delete</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#tempfile.TemporaryDirectory" title="Link to this definition">¶</a></dt>
  323. <dd><p>This class securely creates a temporary directory using the same rules as <a class="reference internal" href="#tempfile.mkdtemp" title="tempfile.mkdtemp"><code class="xref py py-func docutils literal notranslate"><span class="pre">mkdtemp()</span></code></a>.
  324. The resulting object can be used as a <a class="reference internal" href="../glossary.html#term-context-manager"><span class="xref std std-term">context manager</span></a> (see
  325. <a class="reference internal" href="#tempfile-examples"><span class="std std-ref">Examples</span></a>). On completion of the context or destruction
  326. of the temporary directory object, the newly created temporary directory
  327. and all its contents are removed from the filesystem.</p>
  328. <dl class="py attribute">
  329. <dt class="sig sig-object py" id="tempfile.TemporaryDirectory.name">
  330. <span class="sig-name descname"><span class="pre">name</span></span><a class="headerlink" href="#tempfile.TemporaryDirectory.name" title="Link to this definition">¶</a></dt>
  331. <dd><p>The directory name can be retrieved from the <code class="xref py py-attr docutils literal notranslate"><span class="pre">name</span></code> attribute of the
  332. returned object. When the returned object is used as a <a class="reference internal" href="../glossary.html#term-context-manager"><span class="xref std std-term">context manager</span></a>, the
  333. <code class="xref py py-attr docutils literal notranslate"><span class="pre">name</span></code> will be assigned to the target of the <code class="xref std std-keyword docutils literal notranslate"><span class="pre">as</span></code> clause in
  334. the <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, if there is one.</p>
  335. </dd></dl>
  336. <dl class="py method">
  337. <dt class="sig sig-object py" id="tempfile.TemporaryDirectory.cleanup">
  338. <span class="sig-name descname"><span class="pre">cleanup</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#tempfile.TemporaryDirectory.cleanup" title="Link to this definition">¶</a></dt>
  339. <dd><p>The directory can be explicitly cleaned up by calling the
  340. <code class="xref py py-meth docutils literal notranslate"><span class="pre">cleanup()</span></code> method. If <em>ignore_cleanup_errors</em> is true, any unhandled
  341. exceptions during explicit or implicit cleanup (such as a
  342. <a class="reference internal" href="exceptions.html#PermissionError" title="PermissionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">PermissionError</span></code></a> removing open files on Windows) will be ignored,
  343. and the remaining removable items deleted on a “best-effort” basis.
  344. Otherwise, errors will be raised in whatever context cleanup occurs
  345. (the <code class="xref py py-meth docutils literal notranslate"><span class="pre">cleanup()</span></code> call, exiting the context manager, when the object
  346. is garbage-collected or during interpreter shutdown).</p>
  347. </dd></dl>
  348. <p>The <em>delete</em> parameter can be used to disable cleanup of the directory tree
  349. upon exiting the context. While it may seem unusual for a context manager
  350. to disable the action taken when exiting the context, it can be useful during
  351. debugging or when you need your cleanup behavior to be conditional based on
  352. other logic.</p>
  353. <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">tempfile.mkdtemp</span></code> with argument <code class="docutils literal notranslate"><span class="pre">fullpath</span></code>.</p>
  354. <div class="versionadded">
  355. <p><span class="versionmodified added">New in version 3.2.</span></p>
  356. </div>
  357. <div class="versionchanged">
  358. <p><span class="versionmodified changed">Changed in version 3.10: </span>Added <em>ignore_cleanup_errors</em> parameter.</p>
  359. </div>
  360. <div class="versionchanged">
  361. <p><span class="versionmodified changed">Changed in version 3.12: </span>Added the <em>delete</em> parameter.</p>
  362. </div>
  363. </dd></dl>
  364. <dl class="py function">
  365. <dt class="sig sig-object py" id="tempfile.mkstemp">
  366. <span class="sig-prename descclassname"><span class="pre">tempfile.</span></span><span class="sig-name descname"><span class="pre">mkstemp</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">suffix</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">prefix</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</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">text</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="#tempfile.mkstemp" title="Link to this definition">¶</a></dt>
  367. <dd><p>Creates a temporary file in the most secure manner possible. There are
  368. no race conditions in the file’s creation, assuming that the platform
  369. properly implements the <a class="reference internal" href="os.html#os.O_EXCL" title="os.O_EXCL"><code class="xref py py-const docutils literal notranslate"><span class="pre">os.O_EXCL</span></code></a> flag for <a class="reference internal" href="os.html#os.open" title="os.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.open()</span></code></a>. The
  370. file is readable and writable only by the creating user ID. If the
  371. platform uses permission bits to indicate whether a file is executable,
  372. the file is executable by no one. The file descriptor is not inherited
  373. by child processes.</p>
  374. <p>Unlike <a class="reference internal" href="#tempfile.TemporaryFile" title="tempfile.TemporaryFile"><code class="xref py py-func docutils literal notranslate"><span class="pre">TemporaryFile()</span></code></a>, the user of <a class="reference internal" href="#tempfile.mkstemp" title="tempfile.mkstemp"><code class="xref py py-func docutils literal notranslate"><span class="pre">mkstemp()</span></code></a> is responsible
  375. for deleting the temporary file when done with it.</p>
  376. <p>If <em>suffix</em> is not <code class="docutils literal notranslate"><span class="pre">None</span></code>, the file name will end with that suffix,
  377. otherwise there will be no suffix. <a class="reference internal" href="#tempfile.mkstemp" title="tempfile.mkstemp"><code class="xref py py-func docutils literal notranslate"><span class="pre">mkstemp()</span></code></a> does not put a dot
  378. between the file name and the suffix; if you need one, put it at the
  379. beginning of <em>suffix</em>.</p>
  380. <p>If <em>prefix</em> is not <code class="docutils literal notranslate"><span class="pre">None</span></code>, the file name will begin with that prefix;
  381. otherwise, a default prefix is used. The default is the return value of
  382. <a class="reference internal" href="#tempfile.gettempprefix" title="tempfile.gettempprefix"><code class="xref py py-func docutils literal notranslate"><span class="pre">gettempprefix()</span></code></a> or <a class="reference internal" href="#tempfile.gettempprefixb" title="tempfile.gettempprefixb"><code class="xref py py-func docutils literal notranslate"><span class="pre">gettempprefixb()</span></code></a>, as appropriate.</p>
  383. <p>If <em>dir</em> is not <code class="docutils literal notranslate"><span class="pre">None</span></code>, the file will be created in that directory;
  384. otherwise, a default directory is used. The default directory is chosen
  385. from a platform-dependent list, but the user of the application can
  386. control the directory location by setting the <em>TMPDIR</em>, <em>TEMP</em> or <em>TMP</em>
  387. environment variables. There is thus no guarantee that the generated
  388. filename will have any nice properties, such as not requiring quoting
  389. when passed to external commands via <code class="docutils literal notranslate"><span class="pre">os.popen()</span></code>.</p>
  390. <p>If any of <em>suffix</em>, <em>prefix</em>, and <em>dir</em> are not
  391. <code class="docutils literal notranslate"><span class="pre">None</span></code>, they must be the same type.
  392. If they are bytes, the returned name will be bytes instead of str.
  393. If you want to force a bytes return value with otherwise default behavior,
  394. pass <code class="docutils literal notranslate"><span class="pre">suffix=b''</span></code>.</p>
  395. <p>If <em>text</em> is specified and true, the file is opened in text mode.
  396. Otherwise, (the default) the file is opened in binary mode.</p>
  397. <p><a class="reference internal" href="#tempfile.mkstemp" title="tempfile.mkstemp"><code class="xref py py-func docutils literal notranslate"><span class="pre">mkstemp()</span></code></a> returns a tuple containing an OS-level handle to an open
  398. file (as would be returned by <a class="reference internal" href="os.html#os.open" title="os.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.open()</span></code></a>) and the absolute pathname
  399. of that file, in that order.</p>
  400. <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">tempfile.mkstemp</span></code> with argument <code class="docutils literal notranslate"><span class="pre">fullpath</span></code>.</p>
  401. <div class="versionchanged">
  402. <p><span class="versionmodified changed">Changed in version 3.5: </span><em>suffix</em>, <em>prefix</em>, and <em>dir</em> may now be supplied in bytes in order to
  403. obtain a bytes return value. Prior to this, only str was allowed.
  404. <em>suffix</em> and <em>prefix</em> now accept and default to <code class="docutils literal notranslate"><span class="pre">None</span></code> to cause
  405. an appropriate default value to be used.</p>
  406. </div>
  407. <div class="versionchanged">
  408. <p><span class="versionmodified changed">Changed in version 3.6: </span>The <em>dir</em> parameter now accepts a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p>
  409. </div>
  410. </dd></dl>
  411. <dl class="py function">
  412. <dt class="sig sig-object py" id="tempfile.mkdtemp">
  413. <span class="sig-prename descclassname"><span class="pre">tempfile.</span></span><span class="sig-name descname"><span class="pre">mkdtemp</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">suffix</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">prefix</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</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="#tempfile.mkdtemp" title="Link to this definition">¶</a></dt>
  414. <dd><p>Creates a temporary directory in the most secure manner possible. There
  415. are no race conditions in the directory’s creation. The directory is
  416. readable, writable, and searchable only by the creating user ID.</p>
  417. <p>The user of <a class="reference internal" href="#tempfile.mkdtemp" title="tempfile.mkdtemp"><code class="xref py py-func docutils literal notranslate"><span class="pre">mkdtemp()</span></code></a> is responsible for deleting the temporary
  418. directory and its contents when done with it.</p>
  419. <p>The <em>prefix</em>, <em>suffix</em>, and <em>dir</em> arguments are the same as for
  420. <a class="reference internal" href="#tempfile.mkstemp" title="tempfile.mkstemp"><code class="xref py py-func docutils literal notranslate"><span class="pre">mkstemp()</span></code></a>.</p>
  421. <p><a class="reference internal" href="#tempfile.mkdtemp" title="tempfile.mkdtemp"><code class="xref py py-func docutils literal notranslate"><span class="pre">mkdtemp()</span></code></a> returns the absolute pathname of the new directory.</p>
  422. <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">tempfile.mkdtemp</span></code> with argument <code class="docutils literal notranslate"><span class="pre">fullpath</span></code>.</p>
  423. <div class="versionchanged">
  424. <p><span class="versionmodified changed">Changed in version 3.5: </span><em>suffix</em>, <em>prefix</em>, and <em>dir</em> may now be supplied in bytes in order to
  425. obtain a bytes return value. Prior to this, only str was allowed.
  426. <em>suffix</em> and <em>prefix</em> now accept and default to <code class="docutils literal notranslate"><span class="pre">None</span></code> to cause
  427. an appropriate default value to be used.</p>
  428. </div>
  429. <div class="versionchanged">
  430. <p><span class="versionmodified changed">Changed in version 3.6: </span>The <em>dir</em> parameter now accepts a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p>
  431. </div>
  432. <div class="versionchanged">
  433. <p><span class="versionmodified changed">Changed in version 3.12: </span><a class="reference internal" href="#tempfile.mkdtemp" title="tempfile.mkdtemp"><code class="xref py py-func docutils literal notranslate"><span class="pre">mkdtemp()</span></code></a> now always returns an absolute path, even if <em>dir</em> is relative.</p>
  434. </div>
  435. </dd></dl>
  436. <dl class="py function">
  437. <dt class="sig sig-object py" id="tempfile.gettempdir">
  438. <span class="sig-prename descclassname"><span class="pre">tempfile.</span></span><span class="sig-name descname"><span class="pre">gettempdir</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#tempfile.gettempdir" title="Link to this definition">¶</a></dt>
  439. <dd><p>Return the name of the directory used for temporary files. This
  440. defines the default value for the <em>dir</em> argument to all functions
  441. in this module.</p>
  442. <p>Python searches a standard list of directories to find one which
  443. the calling user can create files in. The list is:</p>
  444. <ol class="arabic simple">
  445. <li><p>The directory named by the <span class="target" id="index-1"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">TMPDIR</span></code> environment variable.</p></li>
  446. <li><p>The directory named by the <span class="target" id="index-2"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">TEMP</span></code> environment variable.</p></li>
  447. <li><p>The directory named by the <span class="target" id="index-3"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">TMP</span></code> environment variable.</p></li>
  448. <li><p>A platform-specific location:</p>
  449. <ul class="simple">
  450. <li><p>On Windows, the directories <code class="file docutils literal notranslate"><span class="pre">C:\TEMP</span></code>, <code class="file docutils literal notranslate"><span class="pre">C:\TMP</span></code>,
  451. <code class="file docutils literal notranslate"><span class="pre">\TEMP</span></code>, and <code class="file docutils literal notranslate"><span class="pre">\TMP</span></code>, in that order.</p></li>
  452. <li><p>On all other platforms, the directories <code class="file docutils literal notranslate"><span class="pre">/tmp</span></code>, <code class="file docutils literal notranslate"><span class="pre">/var/tmp</span></code>, and
  453. <code class="file docutils literal notranslate"><span class="pre">/usr/tmp</span></code>, in that order.</p></li>
  454. </ul>
  455. </li>
  456. <li><p>As a last resort, the current working directory.</p></li>
  457. </ol>
  458. <p>The result of this search is cached, see the description of
  459. <a class="reference internal" href="#tempfile.tempdir" title="tempfile.tempdir"><code class="xref py py-data docutils literal notranslate"><span class="pre">tempdir</span></code></a> below.</p>
  460. <div class="versionchanged">
  461. <p><span class="versionmodified changed">Changed in version 3.10: </span>Always returns a str. Previously it would return any <a class="reference internal" href="#tempfile.tempdir" title="tempfile.tempdir"><code class="xref py py-data docutils literal notranslate"><span class="pre">tempdir</span></code></a>
  462. value regardless of type so long as it was not <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  463. </div>
  464. </dd></dl>
  465. <dl class="py function">
  466. <dt class="sig sig-object py" id="tempfile.gettempdirb">
  467. <span class="sig-prename descclassname"><span class="pre">tempfile.</span></span><span class="sig-name descname"><span class="pre">gettempdirb</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#tempfile.gettempdirb" title="Link to this definition">¶</a></dt>
  468. <dd><p>Same as <a class="reference internal" href="#tempfile.gettempdir" title="tempfile.gettempdir"><code class="xref py py-func docutils literal notranslate"><span class="pre">gettempdir()</span></code></a> but the return value is in bytes.</p>
  469. <div class="versionadded">
  470. <p><span class="versionmodified added">New in version 3.5.</span></p>
  471. </div>
  472. </dd></dl>
  473. <dl class="py function">
  474. <dt class="sig sig-object py" id="tempfile.gettempprefix">
  475. <span class="sig-prename descclassname"><span class="pre">tempfile.</span></span><span class="sig-name descname"><span class="pre">gettempprefix</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#tempfile.gettempprefix" title="Link to this definition">¶</a></dt>
  476. <dd><p>Return the filename prefix used to create temporary files. This does not
  477. contain the directory component.</p>
  478. </dd></dl>
  479. <dl class="py function">
  480. <dt class="sig sig-object py" id="tempfile.gettempprefixb">
  481. <span class="sig-prename descclassname"><span class="pre">tempfile.</span></span><span class="sig-name descname"><span class="pre">gettempprefixb</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#tempfile.gettempprefixb" title="Link to this definition">¶</a></dt>
  482. <dd><p>Same as <a class="reference internal" href="#tempfile.gettempprefix" title="tempfile.gettempprefix"><code class="xref py py-func docutils literal notranslate"><span class="pre">gettempprefix()</span></code></a> but the return value is in bytes.</p>
  483. <div class="versionadded">
  484. <p><span class="versionmodified added">New in version 3.5.</span></p>
  485. </div>
  486. </dd></dl>
  487. <p>The module uses a global variable to store the name of the directory
  488. used for temporary files returned by <a class="reference internal" href="#tempfile.gettempdir" title="tempfile.gettempdir"><code class="xref py py-func docutils literal notranslate"><span class="pre">gettempdir()</span></code></a>. It can be
  489. set directly to override the selection process, but this is discouraged.
  490. All functions in this module take a <em>dir</em> argument which can be used
  491. to specify the directory. This is the recommended approach that does
  492. not surprise other unsuspecting code by changing global API behavior.</p>
  493. <dl class="py data">
  494. <dt class="sig sig-object py" id="tempfile.tempdir">
  495. <span class="sig-prename descclassname"><span class="pre">tempfile.</span></span><span class="sig-name descname"><span class="pre">tempdir</span></span><a class="headerlink" href="#tempfile.tempdir" title="Link to this definition">¶</a></dt>
  496. <dd><p>When set to a value other than <code class="docutils literal notranslate"><span class="pre">None</span></code>, this variable defines the
  497. default value for the <em>dir</em> argument to the functions defined in this
  498. module, including its type, bytes or str. It cannot be a
  499. <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p>
  500. <p>If <code class="docutils literal notranslate"><span class="pre">tempdir</span></code> is <code class="docutils literal notranslate"><span class="pre">None</span></code> (the default) at any call to any of the above
  501. functions except <a class="reference internal" href="#tempfile.gettempprefix" title="tempfile.gettempprefix"><code class="xref py py-func docutils literal notranslate"><span class="pre">gettempprefix()</span></code></a> it is initialized following the
  502. algorithm described in <a class="reference internal" href="#tempfile.gettempdir" title="tempfile.gettempdir"><code class="xref py py-func docutils literal notranslate"><span class="pre">gettempdir()</span></code></a>.</p>
  503. <div class="admonition note">
  504. <p class="admonition-title">Note</p>
  505. <p>Beware that if you set <code class="docutils literal notranslate"><span class="pre">tempdir</span></code> to a bytes value, there is a
  506. nasty side effect: The global default return type of
  507. <a class="reference internal" href="#tempfile.mkstemp" title="tempfile.mkstemp"><code class="xref py py-func docutils literal notranslate"><span class="pre">mkstemp()</span></code></a> and <a class="reference internal" href="#tempfile.mkdtemp" title="tempfile.mkdtemp"><code class="xref py py-func docutils literal notranslate"><span class="pre">mkdtemp()</span></code></a> changes to bytes when no
  508. explicit <code class="docutils literal notranslate"><span class="pre">prefix</span></code>, <code class="docutils literal notranslate"><span class="pre">suffix</span></code>, or <code class="docutils literal notranslate"><span class="pre">dir</span></code> arguments of type
  509. str are supplied. Please do not write code expecting or
  510. depending on this. This awkward behavior is maintained for
  511. compatibility with the historical implementation.</p>
  512. </div>
  513. </dd></dl>
  514. <section id="examples">
  515. <span id="tempfile-examples"></span><h2>Examples<a class="headerlink" href="#examples" title="Link to this heading">¶</a></h2>
  516. <p>Here are some examples of typical usage of the <a class="reference internal" href="#module-tempfile" title="tempfile: Generate temporary files and directories."><code class="xref py py-mod docutils literal notranslate"><span class="pre">tempfile</span></code></a> module:</p>
  517. <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">tempfile</span>
  518. <span class="go"># create a temporary file and write some data to it</span>
  519. <span class="gp">&gt;&gt;&gt; </span><span class="n">fp</span> <span class="o">=</span> <span class="n">tempfile</span><span class="o">.</span><span class="n">TemporaryFile</span><span class="p">()</span>
  520. <span class="gp">&gt;&gt;&gt; </span><span class="n">fp</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;Hello world!&#39;</span><span class="p">)</span>
  521. <span class="go"># read data from file</span>
  522. <span class="gp">&gt;&gt;&gt; </span><span class="n">fp</span><span class="o">.</span><span class="n">seek</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
  523. <span class="gp">&gt;&gt;&gt; </span><span class="n">fp</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
  524. <span class="go">b&#39;Hello world!&#39;</span>
  525. <span class="go"># close the file, it will be removed</span>
  526. <span class="gp">&gt;&gt;&gt; </span><span class="n">fp</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  527. <span class="go"># create a temporary file using a context manager</span>
  528. <span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="n">tempfile</span><span class="o">.</span><span class="n">TemporaryFile</span><span class="p">()</span> <span class="k">as</span> <span class="n">fp</span><span class="p">:</span>
  529. <span class="gp">... </span> <span class="n">fp</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;Hello world!&#39;</span><span class="p">)</span>
  530. <span class="gp">... </span> <span class="n">fp</span><span class="o">.</span><span class="n">seek</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
  531. <span class="gp">... </span> <span class="n">fp</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
  532. <span class="go">b&#39;Hello world!&#39;</span>
  533. <span class="gp">&gt;&gt;&gt;</span>
  534. <span class="go"># file is now closed and removed</span>
  535. <span class="go"># create a temporary file using a context manager</span>
  536. <span class="go"># close the file, use the name to open the file again</span>
  537. <span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="n">tempfile</span><span class="o">.</span><span class="n">NamedTemporaryFile</span><span class="p">(</span><span class="n">delete_on_close</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> <span class="k">as</span> <span class="n">fp</span><span class="p">:</span>
  538. <span class="gp">... </span> <span class="n">fp</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;Hello world!&#39;</span><span class="p">)</span>
  539. <span class="gp">... </span> <span class="n">fp</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  540. <span class="gp">... </span><span class="c1"># the file is closed, but not removed</span>
  541. <span class="gp">... </span><span class="c1"># open the file again by using its name</span>
  542. <span class="gp">... </span> <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">fp</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;rb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
  543. <span class="gp">... </span> <span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
  544. <span class="go">b&#39;Hello world!&#39;</span>
  545. <span class="gp">&gt;&gt;&gt;</span>
  546. <span class="go"># file is now removed</span>
  547. <span class="go"># create a temporary directory using the context manager</span>
  548. <span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="n">tempfile</span><span class="o">.</span><span class="n">TemporaryDirectory</span><span class="p">()</span> <span class="k">as</span> <span class="n">tmpdirname</span><span class="p">:</span>
  549. <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;created temporary directory&#39;</span><span class="p">,</span> <span class="n">tmpdirname</span><span class="p">)</span>
  550. <span class="gp">&gt;&gt;&gt;</span>
  551. <span class="go"># directory and contents have been removed</span>
  552. </pre></div>
  553. </div>
  554. </section>
  555. <section id="deprecated-functions-and-variables">
  556. <span id="tempfile-mktemp-deprecated"></span><h2>Deprecated functions and variables<a class="headerlink" href="#deprecated-functions-and-variables" title="Link to this heading">¶</a></h2>
  557. <p>A historical way to create temporary files was to first generate a
  558. file name with the <a class="reference internal" href="#tempfile.mktemp" title="tempfile.mktemp"><code class="xref py py-func docutils literal notranslate"><span class="pre">mktemp()</span></code></a> function and then create a file
  559. using this name. Unfortunately this is not secure, because a different
  560. process may create a file with this name in the time between the call
  561. to <a class="reference internal" href="#tempfile.mktemp" title="tempfile.mktemp"><code class="xref py py-func docutils literal notranslate"><span class="pre">mktemp()</span></code></a> and the subsequent attempt to create the file by the
  562. first process. The solution is to combine the two steps and create the
  563. file immediately. This approach is used by <a class="reference internal" href="#tempfile.mkstemp" title="tempfile.mkstemp"><code class="xref py py-func docutils literal notranslate"><span class="pre">mkstemp()</span></code></a> and the
  564. other functions described above.</p>
  565. <dl class="py function">
  566. <dt class="sig sig-object py" id="tempfile.mktemp">
  567. <span class="sig-prename descclassname"><span class="pre">tempfile.</span></span><span class="sig-name descname"><span class="pre">mktemp</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">suffix</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">''</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">prefix</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'tmp'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dir</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="#tempfile.mktemp" title="Link to this definition">¶</a></dt>
  568. <dd><div class="deprecated">
  569. <p><span class="versionmodified deprecated">Deprecated since version 2.3: </span>Use <a class="reference internal" href="#tempfile.mkstemp" title="tempfile.mkstemp"><code class="xref py py-func docutils literal notranslate"><span class="pre">mkstemp()</span></code></a> instead.</p>
  570. </div>
  571. <p>Return an absolute pathname of a file that did not exist at the time the
  572. call is made. The <em>prefix</em>, <em>suffix</em>, and <em>dir</em> arguments are similar
  573. to those of <a class="reference internal" href="#tempfile.mkstemp" title="tempfile.mkstemp"><code class="xref py py-func docutils literal notranslate"><span class="pre">mkstemp()</span></code></a>, except that bytes file names, <code class="docutils literal notranslate"><span class="pre">suffix=None</span></code>
  574. and <code class="docutils literal notranslate"><span class="pre">prefix=None</span></code> are not supported.</p>
  575. <div class="admonition warning">
  576. <p class="admonition-title">Warning</p>
  577. <p>Use of this function may introduce a security hole in your program. By
  578. the time you get around to doing anything with the file name it returns,
  579. someone else may have beaten you to the punch. <a class="reference internal" href="#tempfile.mktemp" title="tempfile.mktemp"><code class="xref py py-func docutils literal notranslate"><span class="pre">mktemp()</span></code></a> usage can
  580. be replaced easily with <a class="reference internal" href="#tempfile.NamedTemporaryFile" title="tempfile.NamedTemporaryFile"><code class="xref py py-func docutils literal notranslate"><span class="pre">NamedTemporaryFile()</span></code></a>, passing it the
  581. <code class="docutils literal notranslate"><span class="pre">delete=False</span></code> parameter:</p>
  582. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">NamedTemporaryFile</span><span class="p">(</span><span class="n">delete</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
  583. <span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">name</span>
  584. <span class="go">&#39;/tmp/tmptjujjt&#39;</span>
  585. <span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;Hello World!</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
  586. <span class="go">13</span>
  587. <span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  588. <span class="gp">&gt;&gt;&gt; </span><span class="n">os</span><span class="o">.</span><span class="n">unlink</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
  589. <span class="gp">&gt;&gt;&gt; </span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
  590. <span class="go">False</span>
  591. </pre></div>
  592. </div>
  593. </div>
  594. </dd></dl>
  595. </section>
  596. </section>
  597. <div class="clearer"></div>
  598. </div>
  599. </div>
  600. </div>
  601. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  602. <div class="sphinxsidebarwrapper">
  603. <div>
  604. <h3><a href="../contents.html">Table of Contents</a></h3>
  605. <ul>
  606. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tempfile</span></code> — Generate temporary files and directories</a><ul>
  607. <li><a class="reference internal" href="#examples">Examples</a></li>
  608. <li><a class="reference internal" href="#deprecated-functions-and-variables">Deprecated functions and variables</a></li>
  609. </ul>
  610. </li>
  611. </ul>
  612. </div>
  613. <div>
  614. <h4>Previous topic</h4>
  615. <p class="topless"><a href="filecmp.html"
  616. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">filecmp</span></code> — File and Directory Comparisons</a></p>
  617. </div>
  618. <div>
  619. <h4>Next topic</h4>
  620. <p class="topless"><a href="glob.html"
  621. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">glob</span></code> — Unix style pathname pattern expansion</a></p>
  622. </div>
  623. <div role="note" aria-label="source link">
  624. <h3>This Page</h3>
  625. <ul class="this-page-menu">
  626. <li><a href="../bugs.html">Report a Bug</a></li>
  627. <li>
  628. <a href="https://github.com/python/cpython/blob/main/Doc/library/tempfile.rst"
  629. rel="nofollow">Show Source
  630. </a>
  631. </li>
  632. </ul>
  633. </div>
  634. </div>
  635. <div id="sidebarbutton" title="Collapse sidebar">
  636. <span>«</span>
  637. </div>
  638. </div>
  639. <div class="clearer"></div>
  640. </div>
  641. <div class="related" role="navigation" aria-label="related navigation">
  642. <h3>Navigation</h3>
  643. <ul>
  644. <li class="right" style="margin-right: 10px">
  645. <a href="../genindex.html" title="General Index"
  646. >index</a></li>
  647. <li class="right" >
  648. <a href="../py-modindex.html" title="Python Module Index"
  649. >modules</a> |</li>
  650. <li class="right" >
  651. <a href="glob.html" title="glob — Unix style pathname pattern expansion"
  652. >next</a> |</li>
  653. <li class="right" >
  654. <a href="filecmp.html" title="filecmp — File and Directory Comparisons"
  655. >previous</a> |</li>
  656. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  657. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  658. <li class="switchers">
  659. <div class="language_switcher_placeholder"></div>
  660. <div class="version_switcher_placeholder"></div>
  661. </li>
  662. <li>
  663. </li>
  664. <li id="cpython-language-and-version">
  665. <a href="../index.html">3.12.3 Documentation</a> &#187;
  666. </li>
  667. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  668. <li class="nav-item nav-item-2"><a href="filesys.html" >File and Directory Access</a> &#187;</li>
  669. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">tempfile</span></code> — Generate temporary files and directories</a></li>
  670. <li class="right">
  671. <div class="inline-search" role="search">
  672. <form class="inline-search" action="../search.html" method="get">
  673. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  674. <input type="submit" value="Go" />
  675. </form>
  676. </div>
  677. |
  678. </li>
  679. <li class="right">
  680. <label class="theme-selector-label">
  681. Theme
  682. <select class="theme-selector" oninput="activateTheme(this.value)">
  683. <option value="auto" selected>Auto</option>
  684. <option value="light">Light</option>
  685. <option value="dark">Dark</option>
  686. </select>
  687. </label> |</li>
  688. </ul>
  689. </div>
  690. <div class="footer">
  691. &copy;
  692. <a href="../copyright.html">
  693. Copyright
  694. </a>
  695. 2001-2024, Python Software Foundation.
  696. <br />
  697. This page is licensed under the Python Software Foundation License Version 2.
  698. <br />
  699. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  700. <br />
  701. See <a href="/license.html">History and License</a> for more information.<br />
  702. <br />
  703. The Python Software Foundation is a non-profit corporation.
  704. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  705. <br />
  706. <br />
  707. Last updated on Apr 09, 2024 (13:47 UTC).
  708. <a href="/bugs.html">Found a bug</a>?
  709. <br />
  710. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  711. </div>
  712. </body>
  713. </html>
上海开阖软件有限公司 沪ICP备12045867号-1