Applications often need to save some temporary information, which is not particularly important. It is not necessary to write the information in the configuration file, but cannot be left blank. In this case, you can write the information into the temporary file. In fact, many programs generate a large number of temporary files when running, some are used to save logs, some are used to save some temporary data, and some are used to save some irrelevant settings. In Windows, temporary files are usually stored in this folder: C: \ Documents ents
And Settings \ User \ Local Settings \ temp. In fact, the most common IE browser generates a large number of temporary files when browsing the Web page. These temporary files are generally local copies of the web page we browsed. Python provides a tempfile module to operate on temporary data. Refer to the python manual and introduce the following common methods: tempfile. mkstemp ([suffix = ''[, prefix = 'tmp '[, Dir = none [, text = false])
The mkstemp method is used to create a temporary file. This method is only used to create temporary files and call tempfile. after the mkstemp function is used, the system returns the tuples containing two elements. The first element indicates the security level of the temporary file, and the second element indicates the path of the temporary file. The suffix and prefix parameters indicate the suffix and prefix of the temporary file name. The dir specifies the directory where the temporary file is located. If no directory is specified, the system environment variable tmpdir, set temp or TMP to save the temporary file. The text parameter specifies whether to operate the file in the form of text. The default value is false, indicating that the file is operated in binary format.
Tempfile. mkdtemp ([suffix = ''[, prefix = 'tmp '[, Dir = none])
This function is used to create a temporary folder. The parameter means the same as tempfile. mkdtemp. It returns the absolute path of the Temporary Folder.
Tempfile. mktemp ([suffix = ''[, prefix = 'tmp '[, Dir = none])
Mktemp is used to return the path of a temporary file, but does not create this temporary file.
Tempfile. tempdir
This attribute is used to specify the default folder where the temporary file (folder) is created. If this attribute is not set or set to none, Python returns the following environment variables tmpdir, temp, temp specified directory. If these environment variables are not defined, temporary files will be created in the current working directory.
Tempfile. gettempdir ()
Gettempdir () is used to return the folder path for saving the temporary file.
Tempfile. temporaryfile ([mode = 'W + B '[, bufsize =-1 [, suffix = ''[, prefix = 'tmp' [, Dir = none])
This function returns a file-like object for temporary data storage (actually a temporary file on the disk ). When the file object is closed or del, the temporary file will be deleted from the disk. The mode and bufsize parameters are the same as the open () functions. suffix and prefix specify the suffix and prefix of the temporary file name. DIR is used to set the default storage path for the temporary file. The returned class object has a file attribute that points to the underlying file object for real operations.
Tempfile. namedtemporaryfile ([mode = 'W + B '[, bufsize =-1 [, suffix = ''[, prefix = 'tmp' [, Dir = none [, delete = true])
Tempfile. action of the namedtemporaryfile function and tempfile. temporaryfile is similar, but it only has a delete parameter, used to specify whether to delete temporary files on the disk after the class file object is closed or del (when Delete = true, same behavior as temporaryfile ).
Tempfile. spooledtemporaryfile ([max_size = 0 [, mode = 'W + B '[, bufsize =-1 [, suffix = ''[, prefix = 'tmp '[, dir = none])
The behavior of the tempfile. spooledtemporaryfile function is similar to that of tempfile. temporaryfile. The difference is that when writing data to a class object, the Data Length only reaches the specified size of the max_size parameter, or the fileno () method of the Class Object is called, the data is actually written to the temporary files on the disk.