python tempfile module: generate temporary files and temporary directories

The tempfile module is specifically used to create temporary files and temporary directories. It can run well on both UNIX platforms and Windows platforms.

The commonly used functions in the tempfile module are as follows.
Insert picture description hereTips: 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 not pass them.

The tempfile module also provides two low-level functions, tempfile.mkstemp() and tempfile.mkdtemp(). The four functions described above for creating temporary files and temporary directories are all high-level functions. The high-level functions support automatic cleanup and can be used with the with statement, while these two low-level functions do not. Therefore, it is generally recommended to use high-level functions to create temporary files and temporary directories.

In addition, the tempfile module also provides the tempfile.tempdir property, which can be used to change the system's temporary directory.

The following program demonstrates how to use temporary files and temporary directories:

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:778463939
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
import tempfile
# 创建临时文件
fp = tempfile.TemporaryFile()
print(fp.name)
fp.write('两情若是久长时,'.encode('utf-8'))
fp.write('又岂在朝朝暮暮。'.encode('utf-8'))
# 将文件指针移到开始处,准备读取文件
fp.seek(0)
print(fp.read().decode('utf-8')) # 输出刚才写入的内容
# 关闭文件,该文件将会被自动删除
fp.close()
# 通过with语句创建临时文件,with会自动关闭临时文件
with tempfile.TemporaryFile() as fp:
# 写入内容
fp.write(b'I Love Python!')
# 将文件指针移到开始处,准备读取文件
fp.seek(0)
# 读取文件内容
print(fp.read()) # b'I Love Python!'
# 通过with语句创建临时目录
with tempfile.TemporaryDirectory() as tmpdirname:
print('创建临时目录', tmpdirname)

The above program creates temporary files in two ways:

  • The first method is to manually create a temporary file. After reading and writing the temporary file, you need to actively close it. When the program closes the temporary file, the file will be automatically deleted.
  • The second way is to use the with statement to create a temporary file, so that the with statement will automatically close the temporary file.

The above program also creates a temporary directory at the end. Since the program uses the with statement to manage the temporary directory, the program will also automatically delete the temporary directory.

Run the above program, you can see the following output:

C:\Users\admin\AppData\Local\Temp\tmphvehw9z1
两情若是久长时,又岂在朝朝暮暮。
b'I Love Python!'
创建临时目录C:\Users\admin\AppData\Local\Temp\tmp3sjbnwob

The output of the first line above is the file name of the temporary file generated by the program, and the output of the last line is the directory name of the temporary directory generated by the program. It should be noted that do not look for temporary files or temporary folders, because the temporary files and temporary folders will be deleted when the program exits.

Guess you like

Origin blog.csdn.net/sinat_38682860/article/details/108682012