当您打开文件时,它会存储在一个打开的文件对象中,使您可以访问其中的各种方法,例如读取或写入.
>>> f = open("file0")
>>> f
<open file 'file0', mode 'r' at 0x0000000002E51660>
Run Code Online (Sandbox Code Playgroud)
当然,当你完成后,你应该关闭你的文件,以防止它占用内存空间.
>>> f.close()
>>> f
<closed file 'file0', mode 'r' at 0x0000000002E51660>
Run Code Online (Sandbox Code Playgroud)
这会留下一个已关闭的文件,因此该对象仍然存在,但为了便于阅读,它不再使用空格.但这有什么实际应用吗?它无法读取或写入.它不能用于再次重新打开文件.
>>> f.open()
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
f.open()
AttributeError: 'file' object has no attribute 'open'
>>> open(f)
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
open(f)
TypeError: coercing to Unicode: need string or buffer, file found
Run Code Online (Sandbox Code Playgroud)
除了识别文件对象被引用但是已关闭之外,这个已关闭的文件对象是否有实际用途?