summaryrefslogtreecommitdiff
path: root/devdocs/python~3.12/library%2Ftempfile.html
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
committerCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
commit754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch)
treef1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/python~3.12/library%2Ftempfile.html
new repository
Diffstat (limited to 'devdocs/python~3.12/library%2Ftempfile.html')
-rw-r--r--devdocs/python~3.12/library%2Ftempfile.html103
1 files changed, 103 insertions, 0 deletions
diff --git a/devdocs/python~3.12/library%2Ftempfile.html b/devdocs/python~3.12/library%2Ftempfile.html
new file mode 100644
index 00000000..a1fc3c06
--- /dev/null
+++ b/devdocs/python~3.12/library%2Ftempfile.html
@@ -0,0 +1,103 @@
+ <span id="tempfile-generate-temporary-files-and-directories"></span><h1>tempfile — Generate temporary files and directories</h1> <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> <p>This module creates temporary files and directories. It works on all supported platforms. <a class="reference internal" href="#tempfile.TemporaryFile" title="tempfile.TemporaryFile"><code>TemporaryFile</code></a>, <a class="reference internal" href="#tempfile.NamedTemporaryFile" title="tempfile.NamedTemporaryFile"><code>NamedTemporaryFile</code></a>, <a class="reference internal" href="#tempfile.TemporaryDirectory" title="tempfile.TemporaryDirectory"><code>TemporaryDirectory</code></a>, and <a class="reference internal" href="#tempfile.SpooledTemporaryFile" title="tempfile.SpooledTemporaryFile"><code>SpooledTemporaryFile</code></a> are high-level interfaces which provide automatic cleanup and can be used as <a class="reference internal" href="../glossary#term-context-manager"><span class="xref std std-term">context managers</span></a>. <a class="reference internal" href="#tempfile.mkstemp" title="tempfile.mkstemp"><code>mkstemp()</code></a> and <a class="reference internal" href="#tempfile.mkdtemp" title="tempfile.mkdtemp"><code>mkdtemp()</code></a> are lower-level functions which require manual cleanup.</p> <p>All the user-callable functions and constructors take additional arguments which allow direct control over the location and name of temporary files and directories. Files names used by this module include a string of random characters which allows those files to be securely created in shared temporary directories. To maintain backward compatibility, the argument order is somewhat odd; it is recommended to use keyword arguments for clarity.</p> <p>The module defines the following user-callable items:</p> <dl class="py function"> <dt class="sig sig-object py" id="tempfile.TemporaryFile">
+<code>tempfile.TemporaryFile(mode='w+b', buffering=- 1, encoding=None, newline=None, suffix=None, prefix=None, dir=None, *, errors=None)</code> </dt> <dd>
+<p>Return a <a class="reference internal" href="../glossary#term-file-like-object"><span class="xref std std-term">file-like object</span></a> that can be used as a temporary storage area. The file is created securely, using the same rules as <a class="reference internal" href="#tempfile.mkstemp" title="tempfile.mkstemp"><code>mkstemp()</code></a>. It will be destroyed as soon as it is closed (including an implicit close when the object is garbage collected). Under Unix, the directory entry for the file is either not created at all or is removed immediately after the file is created. Other platforms do not support this; your code should not rely on a temporary file created using this function having or not having a visible name in the file system.</p> <p>The resulting object can be used as a <a class="reference internal" href="../glossary#term-context-manager"><span class="xref std std-term">context manager</span></a> (see <a class="reference internal" href="#tempfile-examples"><span class="std std-ref">Examples</span></a>). On completion of the context or destruction of the file object the temporary file will be removed from the filesystem.</p> <p>The <em>mode</em> parameter defaults to <code>'w+b'</code> so that the file created can be read and written without being closed. Binary mode is used so that it behaves consistently on all platforms without regard for the data that is stored. <em>buffering</em>, <em>encoding</em>, <em>errors</em> and <em>newline</em> are interpreted as for <a class="reference internal" href="functions#open" title="open"><code>open()</code></a>.</p> <p>The <em>dir</em>, <em>prefix</em> and <em>suffix</em> parameters have the same meaning and defaults as with <a class="reference internal" href="#tempfile.mkstemp" title="tempfile.mkstemp"><code>mkstemp()</code></a>.</p> <p>The returned object is a true file object on POSIX platforms. On other platforms, it is a file-like object whose <code>file</code> attribute is the underlying true file object.</p> <p>The <a class="reference internal" href="os#os.O_TMPFILE" title="os.O_TMPFILE"><code>os.O_TMPFILE</code></a> flag is used if it is available and works (Linux-specific, requires Linux kernel 3.11 or later).</p> <p>On platforms that are neither Posix nor Cygwin, TemporaryFile is an alias for NamedTemporaryFile.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>tempfile.mkstemp</code> with argument <code>fullpath</code>.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.5: </span>The <a class="reference internal" href="os#os.O_TMPFILE" title="os.O_TMPFILE"><code>os.O_TMPFILE</code></a> flag is now used if available.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.8: </span>Added <em>errors</em> parameter.</p> </div> </dd>
+</dl> <dl class="py function"> <dt class="sig sig-object py" id="tempfile.NamedTemporaryFile">
+<code>tempfile.NamedTemporaryFile(mode='w+b', buffering=- 1, encoding=None, newline=None, suffix=None, prefix=None, dir=None, delete=True, *, errors=None, delete_on_close=True)</code> </dt> <dd>
+<p>This function operates exactly as <a class="reference internal" href="#tempfile.TemporaryFile" title="tempfile.TemporaryFile"><code>TemporaryFile()</code></a> does, except the following differences:</p> <ul class="simple"> <li>This function returns a file that is guaranteed to have a visible name in the file system.</li> <li>To manage the named file, it extends the parameters of <a class="reference internal" href="#tempfile.TemporaryFile" title="tempfile.TemporaryFile"><code>TemporaryFile()</code></a> with <em>delete</em> and <em>delete_on_close</em> parameters that determine whether and how the named file should be automatically deleted.</li> </ul> <p>The returned object is always a <a class="reference internal" href="../glossary#term-file-like-object"><span class="xref std std-term">file-like object</span></a> whose <code>file</code> attribute is the underlying true file object. This file-like object can be used in a <a class="reference internal" href="../reference/compound_stmts#with"><code>with</code></a> statement, just like a normal file. The name of the temporary file can be retrieved from the <code>name</code> attribute of the returned file-like object. On Unix, unlike with the <a class="reference internal" href="#tempfile.TemporaryFile" title="tempfile.TemporaryFile"><code>TemporaryFile()</code></a>, the directory entry does not get unlinked immediately after the file creation.</p> <p>If <em>delete</em> is true (the default) and <em>delete_on_close</em> is true (the default), the file is deleted as soon as it is closed. If <em>delete</em> is true and <em>delete_on_close</em> is false, the file is deleted on context manager exit only, or else when the <a class="reference internal" href="../glossary#term-file-like-object"><span class="xref std std-term">file-like object</span></a> is finalized. Deletion is not always guaranteed in this case (see <a class="reference internal" href="../reference/datamodel#object.__del__" title="object.__del__"><code>object.__del__()</code></a>). If <em>delete</em> is false, the value of <em>delete_on_close</em> is ignored.</p> <p>Therefore to use the name of the temporary file to reopen the file after closing it, either make sure not to delete the file upon closure (set the <em>delete</em> parameter to be false) or, in case the temporary file is created in a <a class="reference internal" href="../reference/compound_stmts#with"><code>with</code></a> statement, set the <em>delete_on_close</em> parameter to be false. The latter approach is recommended as it provides assistance in automatic cleaning of the temporary file upon the context manager exit.</p> <p>Opening the temporary file again by its name while it is still open works as follows:</p> <ul class="simple"> <li>On POSIX the file can always be opened again.</li> <li>
+<p>On Windows, make sure that at least one of the following conditions are fulfilled:</p> <ul> <li>
+<em>delete</em> is false</li> <li>additional open shares delete access (e.g. by calling <a class="reference internal" href="os#os.open" title="os.open"><code>os.open()</code></a> with the flag <code>O_TEMPORARY</code>)</li> <li>
+<em>delete</em> is true but <em>delete_on_close</em> is false. Note, that in this case the additional opens that do not share delete access (e.g. created via builtin <a class="reference internal" href="functions#open" title="open"><code>open()</code></a>) must be closed before exiting the context manager, else the <a class="reference internal" href="os#os.unlink" title="os.unlink"><code>os.unlink()</code></a> call on context manager exit will fail with a <a class="reference internal" href="exceptions#PermissionError" title="PermissionError"><code>PermissionError</code></a>.</li> </ul> </li> </ul> <p>On Windows, if <em>delete_on_close</em> is false, and the file is created in a directory for which the user lacks delete access, then the <a class="reference internal" href="os#os.unlink" title="os.unlink"><code>os.unlink()</code></a> call on exit of the context manager will fail with a <a class="reference internal" href="exceptions#PermissionError" title="PermissionError"><code>PermissionError</code></a>. This cannot happen when <em>delete_on_close</em> is true because delete access is requested by the open, which fails immediately if the requested access is not granted.</p> <p>On POSIX (only), a process that is terminated abruptly with SIGKILL cannot automatically delete any NamedTemporaryFiles it created.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>tempfile.mkstemp</code> with argument <code>fullpath</code>.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.8: </span>Added <em>errors</em> parameter.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.12: </span>Added <em>delete_on_close</em> parameter.</p> </div> </dd>
+</dl> <dl class="py class"> <dt class="sig sig-object py" id="tempfile.SpooledTemporaryFile">
+<code>class tempfile.SpooledTemporaryFile(max_size=0, mode='w+b', buffering=- 1, encoding=None, newline=None, suffix=None, prefix=None, dir=None, *, errors=None)</code> </dt> <dd>
+<p>This class operates exactly as <a class="reference internal" href="#tempfile.TemporaryFile" title="tempfile.TemporaryFile"><code>TemporaryFile()</code></a> does, except that data is spooled in memory until the file size exceeds <em>max_size</em>, or until the file’s <a class="reference internal" href="io#io.IOBase.fileno" title="io.IOBase.fileno"><code>fileno()</code></a> method is called, at which point the contents are written to disk and operation proceeds as with <a class="reference internal" href="#tempfile.TemporaryFile" title="tempfile.TemporaryFile"><code>TemporaryFile()</code></a>.</p> <dl class="py method"> <dt class="sig sig-object py" id="tempfile.SpooledTemporaryFile.rollover">
+<code>rollover()</code> </dt> <dd>
+<p>The resulting file has one additional method, <code>rollover()</code>, which causes the file to roll over to an on-disk file regardless of its size.</p> </dd>
+</dl> <p>The returned object is a file-like object whose <code>_file</code> attribute is either an <a class="reference internal" href="io#io.BytesIO" title="io.BytesIO"><code>io.BytesIO</code></a> or <a class="reference internal" href="io#io.TextIOWrapper" title="io.TextIOWrapper"><code>io.TextIOWrapper</code></a> object (depending on whether binary or text <em>mode</em> was specified) or a true file object, depending on whether <a class="reference internal" href="#tempfile.SpooledTemporaryFile.rollover" title="tempfile.SpooledTemporaryFile.rollover"><code>rollover()</code></a> has been called. This file-like object can be used in a <a class="reference internal" href="../reference/compound_stmts#with"><code>with</code></a> statement, just like a normal file.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.3: </span>the truncate method now accepts a <em>size</em> argument.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.8: </span>Added <em>errors</em> parameter.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.11: </span>Fully implements the <a class="reference internal" href="io#io.BufferedIOBase" title="io.BufferedIOBase"><code>io.BufferedIOBase</code></a> and <a class="reference internal" href="io#io.TextIOBase" title="io.TextIOBase"><code>io.TextIOBase</code></a> abstract base classes (depending on whether binary or text <em>mode</em> was specified).</p> </div> </dd>
+</dl> <dl class="py class"> <dt class="sig sig-object py" id="tempfile.TemporaryDirectory">
+<code>class tempfile.TemporaryDirectory(suffix=None, prefix=None, dir=None, ignore_cleanup_errors=False, *, delete=True)</code> </dt> <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>mkdtemp()</code></a>. The resulting object can be used as a <a class="reference internal" href="../glossary#term-context-manager"><span class="xref std std-term">context manager</span></a> (see <a class="reference internal" href="#tempfile-examples"><span class="std std-ref">Examples</span></a>). On completion of the context or destruction of the temporary directory object, the newly created temporary directory and all its contents are removed from the filesystem.</p> <dl class="py attribute"> <dt class="sig sig-object py" id="tempfile.TemporaryDirectory.name">
+<code>name</code> </dt> <dd>
+<p>The directory name can be retrieved from the <code>name</code> attribute of the returned object. When the returned object is used as a <a class="reference internal" href="../glossary#term-context-manager"><span class="xref std std-term">context manager</span></a>, the <code>name</code> will be assigned to the target of the <code>as</code> clause in the <a class="reference internal" href="../reference/compound_stmts#with"><code>with</code></a> statement, if there is one.</p> </dd>
+</dl> <dl class="py method"> <dt class="sig sig-object py" id="tempfile.TemporaryDirectory.cleanup">
+<code>cleanup()</code> </dt> <dd>
+<p>The directory can be explicitly cleaned up by calling the <code>cleanup()</code> method. If <em>ignore_cleanup_errors</em> is true, any unhandled exceptions during explicit or implicit cleanup (such as a <a class="reference internal" href="exceptions#PermissionError" title="PermissionError"><code>PermissionError</code></a> removing open files on Windows) will be ignored, and the remaining removable items deleted on a “best-effort” basis. Otherwise, errors will be raised in whatever context cleanup occurs (the <code>cleanup()</code> call, exiting the context manager, when the object is garbage-collected or during interpreter shutdown).</p> </dd>
+</dl> <p>The <em>delete</em> parameter can be used to disable cleanup of the directory tree upon exiting the context. While it may seem unusual for a context manager to disable the action taken when exiting the context, it can be useful during debugging or when you need your cleanup behavior to be conditional based on other logic.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>tempfile.mkdtemp</code> with argument <code>fullpath</code>.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.2.</span></p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.10: </span>Added <em>ignore_cleanup_errors</em> parameter.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.12: </span>Added the <em>delete</em> parameter.</p> </div> </dd>
+</dl> <dl class="py function"> <dt class="sig sig-object py" id="tempfile.mkstemp">
+<code>tempfile.mkstemp(suffix=None, prefix=None, dir=None, text=False)</code> </dt> <dd>
+<p>Creates a temporary file in the most secure manner possible. There are no race conditions in the file’s creation, assuming that the platform properly implements the <a class="reference internal" href="os#os.O_EXCL" title="os.O_EXCL"><code>os.O_EXCL</code></a> flag for <a class="reference internal" href="os#os.open" title="os.open"><code>os.open()</code></a>. The file is readable and writable only by the creating user ID. If the platform uses permission bits to indicate whether a file is executable, the file is executable by no one. The file descriptor is not inherited by child processes.</p> <p>Unlike <a class="reference internal" href="#tempfile.TemporaryFile" title="tempfile.TemporaryFile"><code>TemporaryFile()</code></a>, the user of <a class="reference internal" href="#tempfile.mkstemp" title="tempfile.mkstemp"><code>mkstemp()</code></a> is responsible for deleting the temporary file when done with it.</p> <p>If <em>suffix</em> is not <code>None</code>, the file name will end with that suffix, otherwise there will be no suffix. <a class="reference internal" href="#tempfile.mkstemp" title="tempfile.mkstemp"><code>mkstemp()</code></a> does not put a dot between the file name and the suffix; if you need one, put it at the beginning of <em>suffix</em>.</p> <p>If <em>prefix</em> is not <code>None</code>, the file name will begin with that prefix; otherwise, a default prefix is used. The default is the return value of <a class="reference internal" href="#tempfile.gettempprefix" title="tempfile.gettempprefix"><code>gettempprefix()</code></a> or <a class="reference internal" href="#tempfile.gettempprefixb" title="tempfile.gettempprefixb"><code>gettempprefixb()</code></a>, as appropriate.</p> <p>If <em>dir</em> is not <code>None</code>, the file will be created in that directory; otherwise, a default directory is used. The default directory is chosen from a platform-dependent list, but the user of the application can control the directory location by setting the <em>TMPDIR</em>, <em>TEMP</em> or <em>TMP</em> environment variables. There is thus no guarantee that the generated filename will have any nice properties, such as not requiring quoting when passed to external commands via <code>os.popen()</code>.</p> <p>If any of <em>suffix</em>, <em>prefix</em>, and <em>dir</em> are not <code>None</code>, they must be the same type. If they are bytes, the returned name will be bytes instead of str. If you want to force a bytes return value with otherwise default behavior, pass <code>suffix=b''</code>.</p> <p>If <em>text</em> is specified and true, the file is opened in text mode. Otherwise, (the default) the file is opened in binary mode.</p> <p><a class="reference internal" href="#tempfile.mkstemp" title="tempfile.mkstemp"><code>mkstemp()</code></a> returns a tuple containing an OS-level handle to an open file (as would be returned by <a class="reference internal" href="os#os.open" title="os.open"><code>os.open()</code></a>) and the absolute pathname of that file, in that order.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>tempfile.mkstemp</code> with argument <code>fullpath</code>.</p> <div class="versionchanged"> <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 obtain a bytes return value. Prior to this, only str was allowed. <em>suffix</em> and <em>prefix</em> now accept and default to <code>None</code> to cause an appropriate default value to be used.</p> </div> <div class="versionchanged"> <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#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p> </div> </dd>
+</dl> <dl class="py function"> <dt class="sig sig-object py" id="tempfile.mkdtemp">
+<code>tempfile.mkdtemp(suffix=None, prefix=None, dir=None)</code> </dt> <dd>
+<p>Creates a temporary directory in the most secure manner possible. There are no race conditions in the directory’s creation. The directory is readable, writable, and searchable only by the creating user ID.</p> <p>The user of <a class="reference internal" href="#tempfile.mkdtemp" title="tempfile.mkdtemp"><code>mkdtemp()</code></a> is responsible for deleting the temporary directory and its contents when done with it.</p> <p>The <em>prefix</em>, <em>suffix</em>, and <em>dir</em> arguments are the same as for <a class="reference internal" href="#tempfile.mkstemp" title="tempfile.mkstemp"><code>mkstemp()</code></a>.</p> <p><a class="reference internal" href="#tempfile.mkdtemp" title="tempfile.mkdtemp"><code>mkdtemp()</code></a> returns the absolute pathname of the new directory.</p> <p class="audit-hook">Raises an <a class="reference internal" href="sys#auditing"><span class="std std-ref">auditing event</span></a> <code>tempfile.mkdtemp</code> with argument <code>fullpath</code>.</p> <div class="versionchanged"> <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 obtain a bytes return value. Prior to this, only str was allowed. <em>suffix</em> and <em>prefix</em> now accept and default to <code>None</code> to cause an appropriate default value to be used.</p> </div> <div class="versionchanged"> <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#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.12: </span><a class="reference internal" href="#tempfile.mkdtemp" title="tempfile.mkdtemp"><code>mkdtemp()</code></a> now always returns an absolute path, even if <em>dir</em> is relative.</p> </div> </dd>
+</dl> <dl class="py function"> <dt class="sig sig-object py" id="tempfile.gettempdir">
+<code>tempfile.gettempdir()</code> </dt> <dd>
+<p>Return the name of the directory used for temporary files. This defines the default value for the <em>dir</em> argument to all functions in this module.</p> <p>Python searches a standard list of directories to find one which the calling user can create files in. The list is:</p> <ol class="arabic simple"> <li>The directory named by the <span class="target" id="index-1"></span><code>TMPDIR</code> environment variable.</li> <li>The directory named by the <span class="target" id="index-2"></span><code>TEMP</code> environment variable.</li> <li>The directory named by the <span class="target" id="index-3"></span><code>TMP</code> environment variable.</li> <li>
+<p>A platform-specific location:</p> <ul class="simple"> <li>On Windows, the directories <code>C:\TEMP</code>, <code>C:\TMP</code>, <code>\TEMP</code>, and <code>\TMP</code>, in that order.</li> <li>On all other platforms, the directories <code>/tmp</code>, <code>/var/tmp</code>, and <code>/usr/tmp</code>, in that order.</li> </ul> </li> <li>As a last resort, the current working directory.</li> </ol> <p>The result of this search is cached, see the description of <a class="reference internal" href="#tempfile.tempdir" title="tempfile.tempdir"><code>tempdir</code></a> below.</p> <div class="versionchanged"> <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>tempdir</code></a> value regardless of type so long as it was not <code>None</code>.</p> </div> </dd>
+</dl> <dl class="py function"> <dt class="sig sig-object py" id="tempfile.gettempdirb">
+<code>tempfile.gettempdirb()</code> </dt> <dd>
+<p>Same as <a class="reference internal" href="#tempfile.gettempdir" title="tempfile.gettempdir"><code>gettempdir()</code></a> but the return value is in bytes.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.5.</span></p> </div> </dd>
+</dl> <dl class="py function"> <dt class="sig sig-object py" id="tempfile.gettempprefix">
+<code>tempfile.gettempprefix()</code> </dt> <dd>
+<p>Return the filename prefix used to create temporary files. This does not contain the directory component.</p> </dd>
+</dl> <dl class="py function"> <dt class="sig sig-object py" id="tempfile.gettempprefixb">
+<code>tempfile.gettempprefixb()</code> </dt> <dd>
+<p>Same as <a class="reference internal" href="#tempfile.gettempprefix" title="tempfile.gettempprefix"><code>gettempprefix()</code></a> but the return value is in bytes.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.5.</span></p> </div> </dd>
+</dl> <p>The module uses a global variable to store the name of the directory used for temporary files returned by <a class="reference internal" href="#tempfile.gettempdir" title="tempfile.gettempdir"><code>gettempdir()</code></a>. It can be set directly to override the selection process, but this is discouraged. All functions in this module take a <em>dir</em> argument which can be used to specify the directory. This is the recommended approach that does not surprise other unsuspecting code by changing global API behavior.</p> <dl class="py data"> <dt class="sig sig-object py" id="tempfile.tempdir">
+<code>tempfile.tempdir</code> </dt> <dd>
+<p>When set to a value other than <code>None</code>, this variable defines the default value for the <em>dir</em> argument to the functions defined in this module, including its type, bytes or str. It cannot be a <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p> <p>If <code>tempdir</code> is <code>None</code> (the default) at any call to any of the above functions except <a class="reference internal" href="#tempfile.gettempprefix" title="tempfile.gettempprefix"><code>gettempprefix()</code></a> it is initialized following the algorithm described in <a class="reference internal" href="#tempfile.gettempdir" title="tempfile.gettempdir"><code>gettempdir()</code></a>.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>Beware that if you set <code>tempdir</code> to a bytes value, there is a nasty side effect: The global default return type of <a class="reference internal" href="#tempfile.mkstemp" title="tempfile.mkstemp"><code>mkstemp()</code></a> and <a class="reference internal" href="#tempfile.mkdtemp" title="tempfile.mkdtemp"><code>mkdtemp()</code></a> changes to bytes when no explicit <code>prefix</code>, <code>suffix</code>, or <code>dir</code> arguments of type str are supplied. Please do not write code expecting or depending on this. This awkward behavior is maintained for compatibility with the historical implementation.</p> </div> </dd>
+</dl> <section id="examples"> <span id="tempfile-examples"></span><h2>Examples</h2> <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>tempfile</code></a> module:</p> <pre data-language="python">&gt;&gt;&gt; import tempfile
+
+# create a temporary file and write some data to it
+&gt;&gt;&gt; fp = tempfile.TemporaryFile()
+&gt;&gt;&gt; fp.write(b'Hello world!')
+# read data from file
+&gt;&gt;&gt; fp.seek(0)
+&gt;&gt;&gt; fp.read()
+b'Hello world!'
+# close the file, it will be removed
+&gt;&gt;&gt; fp.close()
+
+# create a temporary file using a context manager
+&gt;&gt;&gt; with tempfile.TemporaryFile() as fp:
+... fp.write(b'Hello world!')
+... fp.seek(0)
+... fp.read()
+b'Hello world!'
+&gt;&gt;&gt;
+# file is now closed and removed
+
+# create a temporary file using a context manager
+# close the file, use the name to open the file again
+&gt;&gt;&gt; with tempfile.NamedTemporaryFile(delete_on_close=False) as fp:
+... fp.write(b'Hello world!')
+... fp.close()
+... # the file is closed, but not removed
+... # open the file again by using its name
+... with open(fp.name, mode='rb') as f:
+... f.read()
+b'Hello world!'
+&gt;&gt;&gt;
+# file is now removed
+
+# create a temporary directory using the context manager
+&gt;&gt;&gt; with tempfile.TemporaryDirectory() as tmpdirname:
+... print('created temporary directory', tmpdirname)
+&gt;&gt;&gt;
+# directory and contents have been removed
+</pre> </section> <section id="deprecated-functions-and-variables"> <span id="tempfile-mktemp-deprecated"></span><h2>Deprecated functions and variables</h2> <p>A historical way to create temporary files was to first generate a file name with the <a class="reference internal" href="#tempfile.mktemp" title="tempfile.mktemp"><code>mktemp()</code></a> function and then create a file using this name. Unfortunately this is not secure, because a different process may create a file with this name in the time between the call to <a class="reference internal" href="#tempfile.mktemp" title="tempfile.mktemp"><code>mktemp()</code></a> and the subsequent attempt to create the file by the first process. The solution is to combine the two steps and create the file immediately. This approach is used by <a class="reference internal" href="#tempfile.mkstemp" title="tempfile.mkstemp"><code>mkstemp()</code></a> and the other functions described above.</p> <dl class="py function"> <dt class="sig sig-object py" id="tempfile.mktemp">
+<code>tempfile.mktemp(suffix='', prefix='tmp', dir=None)</code> </dt> <dd>
+<div class="deprecated"> <p><span class="versionmodified deprecated">Deprecated since version 2.3: </span>Use <a class="reference internal" href="#tempfile.mkstemp" title="tempfile.mkstemp"><code>mkstemp()</code></a> instead.</p> </div> <p>Return an absolute pathname of a file that did not exist at the time the call is made. The <em>prefix</em>, <em>suffix</em>, and <em>dir</em> arguments are similar to those of <a class="reference internal" href="#tempfile.mkstemp" title="tempfile.mkstemp"><code>mkstemp()</code></a>, except that bytes file names, <code>suffix=None</code> and <code>prefix=None</code> are not supported.</p> <div class="admonition warning"> <p class="admonition-title">Warning</p> <p>Use of this function may introduce a security hole in your program. By the time you get around to doing anything with the file name it returns, someone else may have beaten you to the punch. <a class="reference internal" href="#tempfile.mktemp" title="tempfile.mktemp"><code>mktemp()</code></a> usage can be replaced easily with <a class="reference internal" href="#tempfile.NamedTemporaryFile" title="tempfile.NamedTemporaryFile"><code>NamedTemporaryFile()</code></a>, passing it the <code>delete=False</code> parameter:</p> <pre data-language="python">&gt;&gt;&gt; f = NamedTemporaryFile(delete=False)
+&gt;&gt;&gt; f.name
+'/tmp/tmptjujjt'
+&gt;&gt;&gt; f.write(b"Hello World!\n")
+13
+&gt;&gt;&gt; f.close()
+&gt;&gt;&gt; os.unlink(f.name)
+&gt;&gt;&gt; os.path.exists(f.name)
+False
+</pre> </div> </dd>
+</dl> </section> <div class="_attribution">
+ <p class="_attribution-p">
+ &copy; 2001&ndash;2023 Python Software Foundation<br>Licensed under the PSF License.<br>
+ <a href="https://docs.python.org/3.12/library/tempfile.html" class="_attribution-link">https://docs.python.org/3.12/library/tempfile.html</a>
+ </p>
+</div>