python关闭文件描述符问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4599980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python close file descriptor question
提问by Patrick
I think this question is more of a "coding style" rather than technical issue.
我认为这个问题更像是一个“编码风格”而不是技术问题。
Said I have a line of code:
说我有一行代码:
buf = open('test.txt','r').readlines()
...
Will the file descriptor automatically close, or will it stay in the memory? If the file descriptor is not closed, what is the prefer way to close it?
文件描述符会自动关闭,还是会留在内存中?如果文件描述符没有关闭,关闭它的首选方法是什么?
采纳答案by sahhhm
If you assign the file object to a variable, you can explicitly close it using .close()
如果将文件对象分配给变量,则可以使用以下命令显式关闭它 .close()
f = open('test.txt','r')
buf = f.readlines()
f.close()
Alternatively (and more generally preferred), you can use the withkeyword (Python 2.5 and greater) as mentioned in the Python docs:
或者(更普遍的首选),您可以使用Python 文档中with提到的关键字(Python 2.5 及更高版本):
It is good practice to use the
withkeyword when dealing with file objects. This has the advantage that the file is properly closed after its suite finishes, even if an exception is raised on the way. It is also much shorter than writing equivalent try-finally blocks:
with在处理文件对象时使用关键字是一种很好的做法。这样做的好处是 文件在其套件完成后会被正确关闭,即使在途中引发异常。它也比编写等效的 try-finally 块要短得多:
>>> with open('test.txt','r') as f:
... buf = f.readlines()
>>> f.closed
True
回答by Falmarri
It will stay in memory until the garbage collector closes it. You should always explicitly close your file descriptors. Just do something like this:
它会一直留在内存中,直到垃圾收集器关闭它。您应该始终明确关闭文件描述符。只是做这样的事情:
with open('test.txt', 'r') as f:
buf = f.readlines()
回答by John La Rooy
Usually in CPython, the file is closed right away when the reference count drops to zero (although this behaviour is not guaranteed for future versions of CPython)
通常在 CPython 中,当引用计数下降到零时,文件会立即关闭(尽管 CPython 的未来版本不能保证这种行为)
In other implementations, such as Jython, the file won't be closed until it is garbarge collected, which can be a long time later.
在其他实现中,例如 Jython,文件在被垃圾收集之前不会关闭,这可能会在很长时间之后。
It's poor style to have code that works differently depending on the implementation's behaviour.
根据实现的行为而使代码以不同的方式工作是一种糟糕的风格。
If it's just for a quickie script or something you are trying in the interpreter shell it's good enough, but for any sort of production work you should usually use a context manager as in Falmarri's answer
如果它只是用于快速脚本或您在解释器外壳中尝试的东西,那就足够了,但是对于任何类型的生产工作,您通常应该使用上下文管理器,如 Falmarri 的回答
回答by Lennart Regebro
It will be automatically closed, but it depends on implementation exactly when. It's nicer to explicitly use a with-block, but if you are just writing a small script for yourself that you run occasionally it doesn't really matter.
它会自动关闭,但这取决于具体何时实现。显式使用 with-block 会更好,但是如果您只是为自己编写一个偶尔运行的小脚本,那么这并不重要。

