问题描述
在2.7中创建代码(对于我正在学习的Python类),并在3.x中交叉测试(对我来说).不需要此代码.我决定构建对将来可能有用的对象,而不是直接回答原始问题.
Creating code in 2.7 (for a Python class I am taking) and cross-testing it in 3.x (for me). This code is not required. I decided to build objects that might be useful for the future instead of simply answering the original question directly.
由于3.x没有要继承的文件对象,因此我的对象在其中使用文件实例,而不是直接从其继承.在测试下面的代码期间,我遇到了一个奇怪的问题.我可以关闭文件,然后再次关闭它,这不会触发任何类型的错误.为了合理地检查此代码是否正确构建,对此有两个问题:*为什么我可以两次关闭同一个文件*我写这个的方式,当我关闭文件时,是真的关闭它并释放文件对象占用的内存吗?
Since 3.x has no file object to inherit, my object uses file instances within it instead of inheriting from it directly. During testing of the code which follows, I encountered an oddity. I can close a file and then close it again and this does not trigger any kind of error. To sanity checking this code is being built right, two questions about this:* why can I close the same file twice* the way I have written this, when I close the file, am I really closing it and freeing up the memory the file object is taking up?
这是代码:
class FileData(object):
def __init__(self, xfilename, xmode):
self.filename = xfilename
self.mode = xmode
self.f = open(xfilename, 'r')
def getAllFileData(self):
self.lines = f.readlines()
self.f.close()
def getLineFromFile(self):
if f.closed:
self.f = open(xfilename, 'r')
self.f.readline()
def fileHead(self, numRows):
if self.f.closed:
self.f = open(xfilename, 'r')
for i in range(numRows):
print(self.f.readline())
self.f.close()
然后我运行了这些测试行,并意外地重新运行了该行以多次关闭文件.为了确保我没有丢失任何东西,后来我在Jupyter单元中组织了这些行并将它们放在一起.
Then I ran these test lines and accidentally re-ran the line to close the file multiple times. To make sure I wasn't missing something, I later organized these lines in a Jupyter cell and ran them together.
chatLog = FileData("script/record.txt", "r")
chatLog.fileHead(15)
chatLog.f.close()
chatLog.f.close()
请注意,fileHead()完成后也会关闭文件,因此上述代码实际上应该尝试关闭同一文件3次.
Note that fileHead() also closes the file when done, so really above code should have tried to close the same file 3 times.
为什么没有错误?并以我认为的方式关闭文件吗?学习语言,以便您进行任何输入.
Why no error? And is this closing the file the way I think it is? Learning the language so any input would be appreciated.
推荐答案
f.close()
不会执行任何操作.
您可以对此进行防护,但是看到它有多么复杂,我不建议这样做:
You could protect against it but seeing how complex it is I wouldn't advise to do it:
import _io
def override_close(f):
if f.closed:
raise IOError("already closed error")
else:
_io.TextIOWrapper.close(f)
f = open("foo.c")
f.close = lambda : override_close(f)
print("closing")
f.close()
print("protected")
f.close() # raises IOError
我已经覆盖了 f
对象的 close
方法,因此它检查已关闭的文件并引发异常.
I have overridden the close
method for the f
object so it checks against already closed file and raises an exception.
以您的示例为例,最好的方法是通过使其不从外部直接不可见来从外部隐藏 f
(和其他数据):
For your example, the best way would be to hide f
(and other data) from the outside by making them not directly invisible from the outside:
self.__f = open(...)
因此,调用者无法再(轻松地:)弄乱您的文件句柄了.
so callers cannot mess (easily :)) with your file handle anymore.
这篇关于Python文件对象允许您关闭已经关闭的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!