我正在使用创建一个tmp文件:
from tempfile import mkstemp
我正在尝试在此文件中写入:
tmp_file = mkstemp()
file = open(tmp_file, 'w')
file.write('TEST\n')
确实,我已经关闭文件并正确执行了,但是当我尝试保存tmp文件时,它仍然是空的。它看上去很基本,但是我不知道为什么它不起作用,有什么解释吗?
mkstemp()
返回带有文件描述符和路径的元组。我认为问题在于您写错了路。(您正在编写类似的路径'(5, "/some/path")'
。)您的代码应如下所示:
from tempfile import mkstemp
fd, path = mkstemp()
# use a context manager to open the file at that path and close it again
with open(path, 'w') as f:
f.write('TEST\n')
# close the file descriptor
os.close(fd)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句