为什么即使在关闭临时文件后我仍然可以写入和读取它?
Why am I able to write to and read a tempfile even after closing it?
我正在尝试从我的 python 脚本打开文本编辑器,我注意到一些明显与我对 tempfile.
文档的理解相矛盾的东西
我的实验从 Alex Martelli 的 answer 开始。
我的代码-
import os
import tempfile
import subprocess
f = tempfile.NamedTemporaryFile(mode='w+t', delete=True)
n = f.name
print('Does exist? : {0}'.format(os.path.exists(n)))
f.close()
print('Does exist? : {0}'.format(os.path.exists(n)))
subprocess.run(['nano', n])
with open(n) as f:
print (f.read())
print('Does exist? : {0}'.format(os.path.exists(n)))
输出:
Does exist? : True
Does exist? : False
Hello from temp file.
Does exist? : True
在代码中,我在用 delete=True
声明的文件对象上显式调用了 close
,但即便如此我也能够向其写入和读取内容。我不明白为什么会这样。
根据文档-
If delete is true (the default), the file is deleted as soon as it is closed.
如果调用 close
删除了文件,那么我不应该先写再读。但它会显示 正确的 您在 nano
执行时输入的文件的内容。和 tempfile 一样,该文件在我打开终端和 运行 脚本的目录中不可见。
更 st运行ge 的是 os.path.exists
前两次工作正常,可能不正确 第三次。
我在这里错过了什么吗?
附加实验:
如果我 运行 下面的代码,那么我可以清楚地看到创建的文件。但这在原始代码中不会发生。
n = '.temp'
subprocess.run(['nano', n])
with open(n) as f:
print (f.read())
print('Does exist? : {0}'.format(os.path.exists(n)))
让我们更深入地了解您的代码。
首先创建临时文件
f = tempfile.NamedTemporaryFile(mode='w+t', delete=True)
n = f.name
print('Does exist? : {0}'.format(os.path.exists(n)))
和这个输出
Does exist? : True
所以没有什么可担心的。然后在接下来的语句中
f.close()
print('Does exist? : {0}'.format(os.path.exists(n)))
您正在关闭文件,实际上文件已被删除,因为您得到以下输出:
Does exist? : False
之后您将通过
重新创建您的文件
subprocess.run(['nano', n])
with open(n) as f:
print (f.read())
所以这就是为什么之后命令
print('Does exist? : {0}'.format(os.path.exists(n)))
returns
Does exist? : True
我正在尝试从我的 python 脚本打开文本编辑器,我注意到一些明显与我对 tempfile.
文档的理解相矛盾的东西我的实验从 Alex Martelli 的 answer 开始。
我的代码-
import os
import tempfile
import subprocess
f = tempfile.NamedTemporaryFile(mode='w+t', delete=True)
n = f.name
print('Does exist? : {0}'.format(os.path.exists(n)))
f.close()
print('Does exist? : {0}'.format(os.path.exists(n)))
subprocess.run(['nano', n])
with open(n) as f:
print (f.read())
print('Does exist? : {0}'.format(os.path.exists(n)))
输出:
Does exist? : True
Does exist? : False
Hello from temp file.
Does exist? : True
在代码中,我在用 delete=True
声明的文件对象上显式调用了 close
,但即便如此我也能够向其写入和读取内容。我不明白为什么会这样。
根据文档-
If delete is true (the default), the file is deleted as soon as it is closed.
如果调用 close
删除了文件,那么我不应该先写再读。但它会显示 正确的 您在 nano
执行时输入的文件的内容。和 tempfile 一样,该文件在我打开终端和 运行 脚本的目录中不可见。
更 st运行ge 的是 os.path.exists
前两次工作正常,可能不正确 第三次。
我在这里错过了什么吗?
附加实验:
如果我 运行 下面的代码,那么我可以清楚地看到创建的文件。但这在原始代码中不会发生。
n = '.temp'
subprocess.run(['nano', n])
with open(n) as f:
print (f.read())
print('Does exist? : {0}'.format(os.path.exists(n)))
让我们更深入地了解您的代码。
首先创建临时文件
f = tempfile.NamedTemporaryFile(mode='w+t', delete=True)
n = f.name
print('Does exist? : {0}'.format(os.path.exists(n)))
和这个输出
Does exist? : True
所以没有什么可担心的。然后在接下来的语句中
f.close()
print('Does exist? : {0}'.format(os.path.exists(n)))
您正在关闭文件,实际上文件已被删除,因为您得到以下输出:
Does exist? : False
之后您将通过
重新创建您的文件subprocess.run(['nano', n])
with open(n) as f:
print (f.read())
所以这就是为什么之后命令
print('Does exist? : {0}'.format(os.path.exists(n)))
returns
Does exist? : True