Hướng dẫn python spooledtemporaryfile
Source code: Lib/tempfile.py This module creates temporary files and directories. It works on all supported platforms. 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: Return a file-like object that can be used as a temporary storage area. The file is
created securely, using the same rules as 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 The dir, prefix and
suffix parameters have the same meaning and defaults as with The returned object is a true file object on POSIX platforms. On other platforms, it is a file-like object whose The On platforms that are neither Posix nor Cygwin, TemporaryFile is an alias for NamedTemporaryFile. Raises an auditing
event Changed in version 3.5: The Changed in version 3.8: Added errors parameter. This function operates exactly as Raises an auditing event
Changed in version 3.8: Added errors parameter. This class operates exactly as The resulting file has one additional method,
The returned object is a file-like object whose Changed in version 3.3: the truncate
method now accepts a Changed in version 3.8: Added errors parameter. classtempfile. TemporaryDirectory (suffix=None, prefix=None, dir=None, ignore_cleanup_errors=False)¶This class securely creates a temporary directory using the same rules as The directory name can be retrieved from the The directory
can be explicitly cleaned up by calling the Raises an
auditing event New in version 3.2. Changed in version 3.10: Added ignore_cleanup_errors parameter. tempfile. mkstemp (suffix=None, prefix=None, dir=None, text=False)¶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 Unlike If suffix is not
If prefix is not If dir is not If any of suffix, prefix, and dir are not If text is specified and true, the file is opened in text mode. Otherwise, (the default) the file is opened in binary mode.
Raises an auditing event Changed
in version 3.5: suffix, prefix, and dir may now be supplied in bytes in order to obtain a bytes return value. Prior to this, only str was allowed. suffix and prefix now accept and default to Changed in version 3.6: The dir parameter now accepts a path-like object. tempfile. mkdtemp (suffix=None, prefix=None,
dir=None)¶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. The user of The prefix, suffix, and dir arguments are the same as for
Raises an auditing event Changed in version 3.5: suffix, prefix, and dir may now be supplied in bytes in order to obtain a bytes return value. Prior to this, only str was allowed. suffix and prefix now accept and default to Changed in version 3.6: The dir parameter now accepts a path-like object. tempfile. gettempdir ()¶Return the name of the directory used for temporary files. This defines the default value for the dir argument to all functions in this module. Python searches a standard list of directories to find one which the calling user can create files in. The list is:
The result of this search is cached, see the description of Changed in version 3.10: Always returns a str. Previously it would return any tempfile. gettempdirb ()¶Same as New in version 3.5. tempfile. gettempprefix ()¶Return the filename prefix used to create temporary files. This does not contain the directory component. tempfile. gettempprefixb ()¶Same as New in version 3.5. The module uses a global variable to store the name of the directory used for temporary files returned by tempfile. tempdir ¶When set to a value other than If Note Beware that if you set Examples¶Here are
some examples of typical usage of the >>> import tempfile # create a temporary file and write some data to it >>> fp = tempfile.TemporaryFile() >>> fp.write(b'Hello world!') # read data from file >>> fp.seek(0) >>> fp.read() b'Hello world!' # close the file, it will be removed >>> fp.close() # create a temporary file using a context manager >>> with tempfile.TemporaryFile() as fp: ... fp.write(b'Hello world!') ... fp.seek(0) ... fp.read() b'Hello world!' >>> # file is now closed and removed # create a temporary directory using the context manager >>> with tempfile.TemporaryDirectory() as tmpdirname: ... print('created temporary directory', tmpdirname) >>> # directory and contents have been removed Deprecated functions and variables¶A historical way to create temporary files was to first generate a file name with the tempfile. mktemp (suffix='', prefix='tmp', dir=None)¶Deprecated since version 2.3: Use Return an absolute pathname of a file that did not exist at the time the call is
made. The prefix, suffix, and dir arguments are similar to those of Warning 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. >>> f = NamedTemporaryFile(delete=False) >>> f.name '/tmp/tmptjujjt' >>> f.write(b"Hello World!\n") 13 >>> f.close() >>> os.unlink(f.name) >>> os.path.exists(f.name) False |