python tempfile module --- generate temporary files and directories


This module is used to create temporary files and directories, and it can be used across platforms. TemporaryFile, NamedTemporaryFile, TemporaryDirectory, and SpooledTemporaryFile are high-level interfaces with automatic cleanup functions that can be used as context managers. mkstemp() and mkdtemp() are low-level functions and need to be cleaned up manually after use.

1 Introduction to tempfile

The commonly used functions in the tempfile module are shown in the following table.

tempfile module functions Function description
tempfile.TemporaryFile(mode=‘w+b’, buffering=None, encoding=None, newline=None, suffix=None, prefix=None, dir=None) Create temporary files. This function returns a file-like object, which supports file I/O.
tempfile.NamedTemporaryFile(mode=‘w+b’, buffering=None, encoding=None, newline=None, suffix=None, prefix=None, dir=None, delete=True) Create temporary files. The function of this function is roughly the same as that of the previous function, except that the temporary file it generates has a file name in the file system.
tempfile.SpooledTemporaryFile(max_size=0, mode=‘w+b’, buffering=None, encoding=None, newline=None, suffix=None, prefix=None, dir=None) Create temporary files. Compared with the TemporaryFile function, when the program outputs data to the temporary file, it will first be output to the memory, and it will be output to the physical disk until max_size is exceeded.
tempfile.TemporaryDirectory(suffix=None, prefix=None, dir=None) Generate a temporary directory.
tempfile.gettempdir() Get the temporary directory of the system.
tempfile.gettempdirb() Same as gettempdir(), except that the function returns a byte string.
tempfile.gettempprefix() Returns the prefix name used to generate temporary files.
tempfile.gettempprefixb() Same as gettempprefix(), except that the function returns a byte string.

Tip: Some functions in the table contain many parameters, but these parameters have their own default values, so if there is no special requirement, you can pass them without parameters.

2 Create temporary files

2.1 TemporaryFile

This function returns a file-like object for temporary data storage (actually corresponding to a temporary file on the disk).

def TemporaryFile(mode='w+b', buffering=-1, encoding=None,
                      newline=None, suffix=None, prefix=None,
                      dir=None, *, errors=None)

The generated object can be used as a context manager. After completing the context or destruction of the file object (the file object is closed or del), the temporary file will be deleted from the file system.

  • The default value of the mode parameter is w+bin binary mode, which can be read and written
  • buffering is used to set the buffering strategy. 0: disable buffering (only allowed in binary mode), 1: line buffering (available only in text mode), integer >1: specify the size of the block buffer (in bytes). If no buffering parameter is given, the default buffering strategy is used
  • encoding is the name of the encoding used to decode or encode the file
  • prefix specifies the prefix of the temporary file name
  • suffix specifies the suffix of the temporary file name
  • dir is used to set the default save path for temporary files
  • The returned file-like object has a file attribute, which points to the underlying file object of the actual operation
from tempfile import TemporaryFile

temp = TemporaryFile(dir='/home/skx/pra')
print(temp)
print(temp.name)

'''
TemporaryFile类的构造方法,其返回的还是一个文件对象。但这个文件对象特殊的地方在于
1. 对应的文件没有文件名,对除了本程序之外的程序不可见
2. 在被关闭的同时被删除
所以上面的两句打印语句,输出分别是一个文件对象,以及一个<fdopen>(并不是文件名)
'''
# 向临时文件中写入内容
temp.write(b'hello\nworld')
temp.seek(0)  # 将文件指针移动到头部,准备读取文件
print(temp.read())
temp.close()  # 关闭文件的同时删除文件

# 通过with语句创建临时文件,with会自动关闭临时文件
with TemporaryFile() as fd:
    fd.write("我最棒".encode('utf-8'))
    fd.seek(0)
    print(fd.read().decode('utf-8'))
<_io.BufferedRandom name=3>
3
b'hello\nworld'
我最棒

Note: The default value of the mode parameter is w+bthat each time you write str, you need to convert to binary and then write again. This is very troublesome. You can specify the opening mode as w+so that you can directly read and write str type

Read the configuration file and put it in a temporary file

  • example.ini
[DEFAULT]
ip = 172.0.0.1
port = 22

[bitbucket.org]
user = Atlan
  • conf_tempfile.py
#coding=utf-8
import configparser
from tempfile import TemporaryFile

conf = configparser.ConfigParser()
conf.read('example.ini')
with TemporaryFile(mode='w+') as fd:
    conf.write(fd)          # 注意这里的用法
    fd.seek(0)
    print(fd.read())

2.2 NamedTemporaryFile

The operation performed by this function is exactly the same as TemporaryFile(), but the temporary file created has a file name, which can be found in the file system, so it can be accessed by multiple processes at the same time

def NamedTemporaryFile(mode='w+b', buffering=-1, encoding=None,
                       newline=None, suffix=None, prefix=None,
                       dir=None, delete=True, *, errors=None)

This function has an additional delete parameter, which is used to specify whether the temporary file on the disk is also deleted after the file-like object is closed or del (when delete = True, the behavior is the same as TemporaryFile).

import os
from tempfile import NamedTemporaryFile

# 指定文件以 "head_" 开头,以"_tail"结尾
temp = NamedTemporaryFile(suffix="_tail", prefix="head_", dir='/home/skx/pra',delete=False)
try:
    print('temp:', temp)
    print('temp.name:', temp.name)
finally:
    temp.close()
# 指定了delete,文件对象 close 或者被 del 之后,磁盘文件不会被删除
print('Exists after close:', os.path.exists(temp.name))

operation result:

temp: <tempfile._TemporaryFileWrapper object at 0x7f17ac9bf0b8>
temp.name: /home/skx/pra/head_0dsw2361_tail
Exists after close: True

Specify delete=False, after the file object is closed or del, the temporary files on the disk will not be deleted. You can see the files in the specified directory as follows
Insert picture description here

2.3 SpooledTemporaryFile

tempfile.SpooledTemporaryFile(max_size=0, mode='w+b', buffering=-1, 
                              encoding=None, newline=None, 
                              suffix=None, prefix=None, dir=None, *, errors=None)

The operation performed by this function is exactly the same as TemporaryFile(), but the data will be cached in memory until the file size exceeds max_size or the fileno() method of the file is called, at which time the data will be written to disk.

2.4 mkstemp

tempfile.mkstemp(suffix=None, prefix=None, dir=None, text=False)
  • prefix specifies the prefix of the temporary file name
  • suffix specifies the suffix of the temporary file name
  • dir is used to set the default save path for temporary files
  • text specifies whether to manipulate the file in the form of text, the default is False, which means that the file is manipulated in the form of binary.

mkstemp() returns a tuple, the first element in the tuple is the handle, it is a system-level handle, pointing to an open file (equivalent to the return value of os.open()), the second element is the file Absolute path.

After the file is used, the file will not be automatically cleared, it needs to be cleared manually.

import tempfile
tmp_f = tempfile.mkstemp(dir="/home/skx/pra/")
print(tmp_f)	# (3, '/home/skx/pra/tmp58do2j53')--> tmp_f[0] 是句柄,tmp_f[1] 是文件路径

fd = tmp_f[1]

with open(fd, 'w+') as f:
    f.write('Hello world')
    f.seek(0)
    print(f.read())

operation result

(3, '/home/skx/pra/tmp58do2j53')
Hello world

3 Create a temporary directory

3.1 TemporaryDirectory

tempfile.TemporaryDirectory(suffix=None, prefix=None, dir=None)
  • prefix specifies the prefix of the temporary file name
  • suffix specifies the suffix of the temporary file name
  • dir is used to set the default save path for temporary files

This function will safely create a temporary directory. The object returned by this function can be used as a context manager. After completing the context or destroying the temporary directory object, the newly created temporary directory and all its contents are automatically deleted from the file system.

You can also call the cleanup() method to manually clean up the directory.

import os
import tempfile

with tempfile.TemporaryDirectory(suffix='_tail', prefix='head_') as dir_name:
    print(dir_name)

print(os.path.exists(dir_name))

operation result

/tmp/head_gtbt2gkw_tail
False

3.2 mkdtemp

def mkdtemp(suffix=None, prefix=None, dir=None)
  • prefix specifies the prefix of the temporary file name
  • suffix specifies the suffix of the temporary file name
  • dir is used to set the default save path for temporary files

Create a temporary directory in the safest way. There will be no competition when creating the directory. The directory can only be read, written, and searched by the creator. Returns the absolute path name of the new directory.

After users use up the temporary directory, they need to delete it by themselves .

import os
import tempfile

dir_name = tempfile.mkdtemp(suffix='_tail', prefix='head_', dir='/tmp')
print(dir_name)
# 需要手动清理
os.removedirs(dir_name)

operation result

/tmp/head_kn9uoe1z_tail

Guess you like

Origin blog.csdn.net/happyjacob/article/details/112385665