10.6. tempfile — Generate temporary files and directories¶
This module generates temporary files and directories. It works on all supported platforms.
In version 2.3 of Python, this module was overhauled for enhanced security. It
now provides three new functions, NamedTemporaryFile(), mkstemp(),
and mkdtemp(), which should eliminate all remaining need to use the
insecure mktemp() function. Temporary file names created by this module
no longer contain the process ID; instead a string of six random characters is
used.
Also, all the user-callable functions now take additional arguments which allow direct control over the location and name of temporary files. It is no longer necessary to use the global tempdir and template variables. 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 functions:
-
tempfile.TemporaryFile([mode='w+b'[, bufsize=-1[, suffix=''[, prefix='tmp'[, dir=None]]]]])¶ Return a file-like object that can be used as a temporary storage area. The file is created using
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 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 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. bufsize defaults to-1, meaning that the operating system default is used.The dir, prefix and suffix parameters are passed to
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. This file-like object can be used in awithstatement, just like a normal file.
-
tempfile.NamedTemporaryFile([mode='w+b'[, bufsize=-1[, suffix=''[, prefix='tmp'[, dir=None[, delete=True]]]]]])¶ This function operates exactly as
TemporaryFile()does, except that the file is guaranteed to have a visible name in the file system (on Unix, the directory entry is not unlinked). That name can be retrieved from thenamemember of the file object. Whether the name can be used to open the file a second time, while the named temporary file is still open, varies across platforms (it can be so used on Unix; it cannot on Windows NT or later). If delete is true (the default), the file is deleted as soon as it is closed.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.New in version 2.3:
New in version 2.3.
New in version 2.6:
New in version 2.6: The delete parameter.