Python module: tempfile

Source: Internet
Author: User

"""

@author:P Eng (non-original)

Reference: http://python.jobbole.com/81515/

Record learning process

"""

Applications often want to keep some temporary information, which is not particularly important, it is not necessary to write in the configuration file, but not without, this time can be written to the temporary file. In fact, many programs run at the time, will produce a lot of temporary files, some to save the log, some to save some temporary data, and some save some insignificant settings. In the Windows operating system, temporary files are generally saved in this folder: C:/Documents and Settings/user/local settings/temp. In fact, our most commonly used IE browser when browsing the web, will produce a large number of temporary files, these temporary files are generally we have visited the local copy of the webpage. Python provides a tempfile module for working with temporary data. Refer to the Python manual, which describes 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 a temporary file , after calling the Tempfile.mkstemp function, returns a tuple containing two elements, the first element indicates the security level of the operation of the temporary file, and the second element indicates the path to the temporary file. The parameters suffix and prefix represent the suffix and prefix of the temporary file name respectively; dir Specifies the directory where the temporary file resides, and if no directory is specified, the temp or TMP will be TMPDIRaccording to the system environment variable. Settings to save the temporary file, the parameter text specifies whether the file is manipulated as text, and the default is False, which indicates that the file is manipulated in binary form.

Tempfile.mkdtemp ([suffix= "[, prefix= ' tmp ' [, Dir=none]])

This function is used to create a temporary folder. The meaning of the parameter is the same as tempfile.mkdtemp. It returns the absolute path of the temporary folder.

Tempfile.mktemp ([suffix= "[, prefix= ' tmp ' [, Dir=none]])

The mktemp is used to return the path to a temporary file, but the temporary file is not created.

Tempfile.tempdir

This property is used to specify the default folder where temporary files (folders) are created. If you do not set this property or set it to None,python will return the following environment variable tmpdir, TEMP, temp specified directory, and 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 where the temporary files are saved.

Tempfile. Temporaryfile ([mode= ' w+b ' [, bufsize=-1[, suffix= "[, prefix= ' tmp ' [, Dir=none]]]])

The function returns a class file Object (file-like) for temporary data saving (which actually corresponds to a temporary file on the disk). When the file object is close or del, the temporary file is deleted from the disk. The mode and bufsize parameters are the same as the open () function; suffix and prefix specify the suffix and prefix of the temporary file name; dir is used to set the default save path for temporary files. The returned class file object has one file property that points to the underlying file object of the actual operation.

Tempfile. Namedtemporaryfile ([mode= ' w+b ' [, bufsize=-1[, suffix= "[, prefix= ' tmp ' [, dir=none[, Delete=true]]]]

Tempfile. The behavior of the Namedtemporaryfile function is similar to tempfile.temporaryfile, except that it has a delete parameter that specifies whether the class file object is either close or after del to delete the temporary file on the disk (when the delete = True when the behavior is the same as Temporaryfile).

Tempfile. Spooledtemporaryfile ([max_size=0[, mode= ' w+b ' [, bufsize=-1[, suffix= "[, prefix= ' tmp ' [, Dir=none]]] []]

Tempfile. The behavior of the Spooledtemporaryfile function is similar to Tempfile.temporaryfile. The difference is that when writing data to a class file object, the data is only actually written to the disk's temporary file when it reaches the parameter max_size the specified size, or when the Fileno () method of the class file object is called.

Python module: tempfile

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.