This is the sixth day of my participation in the August More text Challenge. For details, see:August is more challenging

Retrospective review

Python provides a number of file/directory manipulation modules. There are seven common ones, four of which we have already studied

  • ๐Ÿ“ƒ OS module that operates on files/directories,
  • ๐Ÿ“ƒ Os. path module that operates on file paths
  • ๐Ÿ“ƒ FileInput module that reads data streams from multiple files
  • ๐Ÿ“ƒ Shutil module for advanced operations on copying/moving/compressing/decompressing files

Life small scene thinking ๐Ÿค”

We in the daily use of applications, will be generated on the computer to see some do not understand the file, that is why ah?

The reason is that ๐Ÿคช

  • When the APP is running, it saves some temporary information.
  • Temporary information: not important but can’t be absent.
  • Temporary information is used to: keep logs, temporary data, irrelevant Settings, view data copies, and so on
  • Temporary information storage location: In Windows, temporary files are usually stored in C:/Documents and Settings/User/Local Settings/Temp

Therefore, Python provides a tempFile module for manipulating temporary information.

Let’s go~~๐Ÿง‘โœˆ๏ธ

1. Overview of tempFile module

The โญ tempFile module is used to create temporary files and directories for Windows and Linux systems

  • Usage scenario: The tmpFile module enables applications to create temporary data without having to share it with other programs

  • Provides four high-level classes (automatically clear closed files) : TemporaryFile class, NameTemporaryFile class, TemporaryDirectory class, and SpooledTemporaryFile class

    (1) TemporaryFile class: the standard interface provided for the most calls to temporary files, which can be used to read and write temporary files and ensure the hiding of temporary files. The default file opening mode is W + B

    (2) NamedTemporaryFile class: similar to the Temporaryfile class, but for the judgment of whether to delete after the file is closed, the default is True.

    (3) TemporaryDirectory class: a TemporaryDirectory is safely created. The returned object can be used by the context manager.

    (4) SpooledTemporaryFile: similar to Temporaryfile, add a gate value Max_size to restrict the initial writing of files. When the data does not reach max_size, it is temporarily stored in memory. Only when the data exceeds the max_size value will the data be written to the file for saving. By default, Max_size is 0.

  • There are also functions that require manual cleanup: mkstemp() and mkdtemp() methods

โš  ๏ธ note:

  1. The tempFile module provides parameters to set temporary file and temporary directory paths and names
  2. The tempFile module creates filenames that include a string of random characters that are guaranteed to be more secure in a public temporary directory.
  3. The tempfile.mktemp() method has been removed in PYTHon2.3 and replaced by mkstemp()

2. Tempfile module related methods

The tempFile module provides the following method for creating temporary files:

methods role
โ˜€๏ธ tempfile.TemporaryFile(mode=’w+b’, buffering=None, encoding=None, newline=None, suffix=None, prefix=None, dir=None) Create temporary files and support IO
โ˜€ ๏ธ tempfile as expected. NamedTemporaryFile (mode = ‘w + b, the buffering = None, encoding = None, newline = None, suffix = None, prefix = None, dir=None, delete=True) Create temporary file with file name, add delete field default True
โ˜€ ๏ธ tempfile as expected. SpooledTemporaryFile (max_size = 0, mode = ‘w + b, the buffering = None, encoding = None, newline = None, suffix = None. prefix=None, dir=None) Create temporary files. If max_size is exceeded, the disk will be exported
โ˜€ ๏ธ tempfile as expected. TemporaryDirectory (suffix = None, prefix = None, dir = None) Generate temporary directories
tempfile.mkstemp(suffix=None,ย prefix=None,ย dir=None,ย text=False) Create temporary files safely. The return type is tuple. After using temporary files, you need to manually delete them
tempfile.mkdtemp(suffix=None,ย prefix=None,ย dir=None) Create a temporary directory safely and return the absolute path of the new directory. After using the temporary directory, you need to manually delete it
tempfile.gettempdir() Returns the name of the directory of temporary files to be placed
tempfile.gettempprefixb() Returns the filename prefix used to create the temporary file, which does not contain the directory section.

๐Ÿ“ฃ Knock on the blackboard:

  1. Tempfile provides four advanced methods (listed above) that can be used with the context manager with
  2. Tempfile.mkstemp () is opened in binary mode by default
  3. The directory created by tempfile.mktemp() can only be read, written, and retrieved by the author

3. Experiment

๐Ÿ” Constructor of the TemporaryFile class, which again returns a file object. Two notable features are as follows:

  1. The corresponding file has no file name and is not visible to programs other than this program
  2. Deleted while being closed (automatically deletes created files)

๐Ÿ†š After using open() to create a document, you need to call os.remove(file) to delete the file

Import tempfile temp = tempfile.temporaryFile () # print(temp) # print(temp) # print(temp) # Temp. Write (b'hello\nJuejin') Temp.seek (0) print(temp.read()) temp.close() # When the file is closed, the system automatically deletes the fileCopy the code

PS: Data written to the TemporaryFile is written in binary mode by default, so the data should be converted to the corresponding format when written, otherwise the system will throw an exception.

conclusion

In this installment, we learned about the tempFile module for creating temporary file operations. Under this module, four advanced functions are provided, namely TemporaryFile, NameTemporaryFile, TemporaryDirectory and SpooledTemporaryFile, which can be used together with the upper and lower file manager with. In addition, the system will automatically delete the system after the use is complete to release system space.

We’ll see you next time ~๐ŸŒน๐ŸŒน๐ŸŒน๐ŸŒน๐ŸŒน