<bdo id='ttIns'></bdo><ul id='ttIns'></ul>

        <legend id='ttIns'><style id='ttIns'><dir id='ttIns'><q id='ttIns'></q></dir></style></legend>
      1. <i id='ttIns'><tr id='ttIns'><dt id='ttIns'><q id='ttIns'><span id='ttIns'><b id='ttIns'><form id='ttIns'><ins id='ttIns'></ins><ul id='ttIns'></ul><sub id='ttIns'></sub></form><legend id='ttIns'></legend><bdo id='ttIns'><pre id='ttIns'><center id='ttIns'></center></pre></bdo></b><th id='ttIns'></th></span></q></dt></tr></i><div id='ttIns'><tfoot id='ttIns'></tfoot><dl id='ttIns'><fieldset id='ttIns'></fieldset></dl></div>
      2. <tfoot id='ttIns'></tfoot>

        <small id='ttIns'></small><noframes id='ttIns'>

        Python GC 也会关闭文件吗?

        时间:2023-07-05
          <tbody id='iKqnt'></tbody>

          <bdo id='iKqnt'></bdo><ul id='iKqnt'></ul>

          <i id='iKqnt'><tr id='iKqnt'><dt id='iKqnt'><q id='iKqnt'><span id='iKqnt'><b id='iKqnt'><form id='iKqnt'><ins id='iKqnt'></ins><ul id='iKqnt'></ul><sub id='iKqnt'></sub></form><legend id='iKqnt'></legend><bdo id='iKqnt'><pre id='iKqnt'><center id='iKqnt'></center></pre></bdo></b><th id='iKqnt'></th></span></q></dt></tr></i><div id='iKqnt'><tfoot id='iKqnt'></tfoot><dl id='iKqnt'><fieldset id='iKqnt'></fieldset></dl></div>
          1. <legend id='iKqnt'><style id='iKqnt'><dir id='iKqnt'><q id='iKqnt'></q></dir></style></legend>

                • <small id='iKqnt'></small><noframes id='iKqnt'>

                  <tfoot id='iKqnt'></tfoot>
                • 本文介绍了Python GC 也会关闭文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  考虑以下 Python (2.x) 代码:

                  Consider the following piece of Python (2.x) code:

                  for line in open('foo').readlines():
                      print line.rstrip()
                  

                  我假设由于打开的文件仍然未被引用,它必须自动关闭.我已经阅读了 Python 中的垃圾收集器,它释放了未使用对象分配的内存.GC 是否也足以处理文件?

                  I assume that since the open file remains unreferenced it has to be closed automatically. I have read about garbage collector in Python which frees memory allocated by unused objects. Is GC general enough to handle the files too?

                  推荐答案

                  更新

                  对于当前版本的 python,明确的建议是显式关闭文件或使用 with 语句.没有任何迹象表明 GC 将为您关闭文件.所以现在的答案应该是:也许,但不能保证.始终使用 close()with 语句.

                  For current versions of python, the clear recommendation is to close files explicitly or use a with statement. No indication anymore that the GC will close the file for you. So now the answer should be: Maybe, but no guarantee. Always use close() or a with statement.

                  在 Python 3.8 文档中文本已更新为:

                  如果你没有使用 with 关键字,那么你应该调用 f.close() 来关闭文件并立即释放它使用的所有系统资源.

                  If you’re not using the with keyword, then you should call f.close() to close the file and immediately free up any system resources used by it.

                  警告: 调用 f.write() 而不使用 with 关键字或调用 f.close() 可能会导致参数f.write() 没有完全写入磁盘,即使程序成功退出.

                  Warning: Calling f.write() without using the with keyword or calling f.close() might result in the arguments of f.write() not being completely written to the disk, even if the program exits successfully.

                  旧答案:

                  取自 Python 3.6 文档:

                  如果你没有使用 with 关键字,那么你应该调用 f.close() 来关闭文件并立即释放它使用的所有系统资源.如果您没有明确关闭文件,Python 的垃圾收集器最终会销毁该对象并为您关闭打开的文件,但该文件可能会保持打开一段时间.另一个风险是不同的 Python 实现会在不同的时间进行这种清理.

                  If you’re not using the with keyword, then you should call f.close() to close the file and immediately free up any system resources used by it. If you don’t explicitly close a file, Python’s garbage collector will eventually destroy the object and close the open file for you, but the file may stay open for a while. Another risk is that different Python implementations will do this clean-up at different times.

                  所以是的,文件将自动关闭,但为了控制进程,您应该自己执行此操作或使用 with 语句:

                  So yes, the file will be closed automatically, but in order to be in control of the process you should do so yourself or use a with statement:

                  with open('foo') as foo_file:
                      for line in foo_file.readlines():
                          print line.rstrip()
                  

                  foo_file 将在 with 块结束后关闭

                  foo_file will be clsoed once the with block ends

                  在 Python 2.7 文档中,措辞不同:

                  当你处理完一个文件后,调用 f.close() 来关闭它并释放它打开的文件占用的任何系统资源.打电话后f.close(),尝试使用文件对象会自动失败.

                  When you’re done with a file, call f.close() to close it and free up any system resources taken up by the open file. After calling f.close(), attempts to use the file object will automatically fail.

                  所以我假设您不应该依赖垃圾收集器自动为您关闭文件,而只需手动/使用 with

                  so I assume that you should not depend on the garbage collector automatically closing files for you and just do it manually/use with

                  这篇关于Python GC 也会关闭文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 Python 中删除对象的每个引用? 下一篇:复合对象上的python垃圾收集器行为

                  相关文章

                • <small id='mlJz2'></small><noframes id='mlJz2'>

                  <tfoot id='mlJz2'></tfoot>
                  <i id='mlJz2'><tr id='mlJz2'><dt id='mlJz2'><q id='mlJz2'><span id='mlJz2'><b id='mlJz2'><form id='mlJz2'><ins id='mlJz2'></ins><ul id='mlJz2'></ul><sub id='mlJz2'></sub></form><legend id='mlJz2'></legend><bdo id='mlJz2'><pre id='mlJz2'><center id='mlJz2'></center></pre></bdo></b><th id='mlJz2'></th></span></q></dt></tr></i><div id='mlJz2'><tfoot id='mlJz2'></tfoot><dl id='mlJz2'><fieldset id='mlJz2'></fieldset></dl></div>

                    <bdo id='mlJz2'></bdo><ul id='mlJz2'></ul>

                    1. <legend id='mlJz2'><style id='mlJz2'><dir id='mlJz2'><q id='mlJz2'></q></dir></style></legend>