tempfile — Generate temporary files and directories¶
Source code: Lib/tempfile.py
This module creates temporary files and directories. It works on all
supported platforms. TemporaryFile, NamedTemporaryFile,
TemporaryDirectory, and SpooledTemporaryFile are high-level
interfaces which provide automatic cleanup and can be used as
context managers. mkstemp() and
mkdtemp() are lower-level functions which require manual cleanup.
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.
The module defines the following user-callable items:
- tempfile.TemporaryFile(mode='w+b', buffering=-1, encoding=None, newline=None, suffix=None, prefix=None, dir=None, *, errors=None)¶
Return a file-like object that can be used as a temporary storage area. The file is created securely, using the same rules as
mkstemp(). 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.The resulting object can be used as a context manager (see Examples). On completion of the context or destruction of the file object the temporary file will be removed from the filesystem.
The mode parameter defaults to
'w+b'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. buffering, encoding, errors and newline are interpreted as foropen().The dir, prefix and suffix parameters have the same meaning and defaults as with
mkstemp().The returned object is a true file object on POSIX platforms. On other platforms, it is a file-like object whose
fileattribute is the underlying true file object.The
os.O_TMPFILEflag is used if it is available and works (Linux-specific, requires Linux kernel 3.11 or later).On platforms that are neither Posix nor Cygwin, TemporaryFile is an alias for NamedTemporaryFile.
Raises an auditing event
tempfile.mkstempwith argumentfullpath.Changed in version 3.5: The
os.O_TMPFILEflag is now used if available.Changed in version 3.8: Added errors parameter.
- 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)¶
This function operates exactly as
TemporaryFile()does, except the following differences:This function returns a file that is guaranteed to have a visible name in the file system.
To manage the named file, it extends the parameters of
TemporaryFile()with delete and delete_on_close parameters that determine whether and how the named file should be automatically deleted.
The returned object is always a file-like object whose
fileattribute is the underlying true file object. This file-like object can be used in awithstatement, just like a normal file. The name of the temporary file can be retrieved from thenameattribute of the returned file-like object. On Unix, unlike with theTemporaryFile(), the directory entry does not get unlinked immediately after the file creation.If delete is true (the default) and delete_on_close is true (the default), the file is deleted as soon as it is closed. If delete is true and delete_on_close is false, the file is deleted on context manager exit only, or else when the file-like object is finalized. Deletion is not always guaranteed in this case (see
object.__del__()). If delete is false, the value of delete_on_close is ignored.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 delete parameter to be false) or, in case the temporary file is created in a
withstatement, set the delete_on_close 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.Opening the temporary file again by its name while it is still open works as follows:
On POSIX the file can always be opened again.
On Windows, make sure that at least one of the following conditions are fulfilled:
delete is false
additional open shares delete access (e.g. by calling
os.open()with the flagO_TEMPORARY)delete is true but delete_on_close is false. Note, that in this case the additional opens that do not share delete access (e.g. created via builtin
open()) must be closed before exiting the context manager, else theos.unlink()call on context manager exit will fail with aPermissionError.
On Windows, if delete_on_close is false, and the file is created in a directory for which the user lacks delete access, then the