Home >Backend Development >Python Tutorial >What is pythontemp
The tempfile module is specifically designed to create temporary files and temporary directories, and it works well on both UNIX and Windows platforms.
The tempfile module provides many commonly used functions. Let me introduce these functions in detail below.
tempfile.TemporaryFile(mode='w b', buffering=None, encoding=None, newline=None, suffix=None, prefix=None, dir=None) : Create a temporary file. This function returns a file-like object, that is, supports file I/O.
tempfile.NamedTemporaryFile(mode='w b', buffering=None, encoding=None, newline=None, suffix=None, prefix=None, dir=None, delete=True): Create a temporary file. This function does much the same thing as the previous function, except that the temporary file it generates has a filename in the file system.
Related recommendations: "Python Video Tutorial"
tempfile.SpooledTemporaryFile(max_size=0, mode='w b', buffering=None, encoding=None, newline =None, suffix=None, prefix=None, dir=None): Create a temporary file. Compared with the TemporaryFile function, when the program outputs data to the temporary file, it will be output to the memory first, and will not be actually 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 system’s temporary directory.
tempfile.gettempdirb(): Same as gettempdir(), except that this function returns a bytestring.
tempfile.gettempprefix(): Returns the prefix used to generate temporary files.
tempfile.gettempprefixb(): Same as gettempprefix(), except that this function returns a bytestring.
The tempfile module also provides two low-level functions, tempfile.mkstemp() and tempfile.mkdtemp(). The four functions introduced above for creating temporary files and temporary directories are all high-level functions. The high-level functions support automatic cleaning 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 attribute, and the system's temporary directory can be changed by assigning a value to this attribute.
The following program demonstrates how to use temporary files and temporary directories:
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:
First The first way 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. Because the program uses the with statement to manage the temporary directory, the program automatically deletes the temporary directory as well.
Run the above program and 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 first line of output above is the file name of the temporary file generated by the program, and the last line of output is the name of the temporary file generated by the program. The directory name of the temporary directory. 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.
The above is the detailed content of What is pythontemp. For more information, please follow other related articles on the PHP Chinese website!