Python os.tmpfile() 方法



Python 方法tmpfile() 返回一个以更新模式 (w+b) 打开的新临时文件对象。该文件没有与其关联的目录条目,并且一旦没有文件描述符,它将被自动删除。

Python os.tmpfile() 方法已弃用。

语法

以下是 Python os.tmpfile() 方法的语法:

os.tmpfile()

参数

Python os.tmpfile() 方法不接受任何参数。

返回值

Python os.tmpfile() 方法返回一个新的临时文件对象

示例

以下示例显示了 tmpfile() 方法的用法。

import os

# The file has no directory entries associated with it and will be
# deleted automatically once there are no file descriptors.
tmpfile = os.tmpfile()
tmpfile.write('Temporary newfile is here.....')
tmpfile.seek(0)

print (tmpfile.read())
tmpfile.close

当我们运行上述程序时,它会产生以下结果:

Temporary newfile is here.....
python_files_io.htm
广告