Python module learning-tempfile temporary file (folder) Operations

Source: Internet
Author: User

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 lot 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 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 usedCreateA temporary file. This methodUsed only to create temporary files, Call the tempfile. mkstemp function, and return 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 variablesTmpdir,TempOrTMPThe parameter text 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 of 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.

A simple and practical module, isn't it?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.