版权声明:本文为博主原创文章,欢迎转载,请注明出处 https://blog.csdn.net/mouday/article/details/84072829
tempfile需要的时候创建零时文件,关闭之后就被删除了
import tempfile
import os
# 创建文件
file = tempfile.TemporaryFile(mode="w+")
print(file.name)
# 4
print(os.path.exists(file.name))
# True
# 写入、读取操作
file.write("hello world")
file.seek(0)
print(file.read())
# hello world
# 关闭资源
file.close()
print(os.path.exists(file.name))
# False
参考: https://docs.python.org/3/library/tempfile.html