1. <tfoot id='aorsl'></tfoot>

          <bdo id='aorsl'></bdo><ul id='aorsl'></ul>
        <legend id='aorsl'><style id='aorsl'><dir id='aorsl'><q id='aorsl'></q></dir></style></legend>

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

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

      2. 显式关闭文件重要吗?

        时间:2023-07-05
            <tfoot id='TyD0K'></tfoot>

              <bdo id='TyD0K'></bdo><ul id='TyD0K'></ul>
                  <tbody id='TyD0K'></tbody>

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

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

                1. 本文介绍了显式关闭文件重要吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在 Python 中,如果您打开文件而不调用 close(),或者关闭文件但不使用 try-finally 或"with" 语句,这是个问题吗?或者作为一种编码实践,依靠 Python 垃圾收集来关闭所有文件就足够了吗?例如,如果这样做:

                  In Python, if you either open a file without calling close(), or close the file but not using try-finally or the "with" statement, is this a problem? Or does it suffice as a coding practice to rely on the Python garbage-collection to close all files? For example, if one does this:

                  for line in open("filename"):
                      # ... do stuff ...
                  

                  ...这是一个问题,因为文件永远无法关闭,并且可能发生阻止它被关闭的异常?还是肯定会因为文件超出范围而在 for 语句结束时关闭?

                  ... is this a problem because the file can never be closed and an exception could occur that prevents it from being closed? Or will it definitely be closed at the conclusion of the for statement because the file goes out of scope?

                  推荐答案

                  在您的示例中,不能保证在解释器退出之前关闭文件.在当前版本的 CPython 中,该文件将在 for 循环结束时关闭,因为 CPython 使用引用计数作为其主要的垃圾收集机制,但这是一个实现细节,而不是该语言的特性.Python 的其他实现不能保证以这种方式工作.例如 IronPython、PyPy 和 Jython 不使用引用计数,因此不会在循环结束时关闭文件.

                  In your example the file isn't guaranteed to be closed before the interpreter exits. In current versions of CPython the file will be closed at the end of the for loop because CPython uses reference counting as its primary garbage collection mechanism but that's an implementation detail, not a feature of the language. Other implementations of Python aren't guaranteed to work this way. For example IronPython, PyPy, and Jython don't use reference counting and therefore won't close the file at the end of the loop.

                  依赖 CPython 的垃圾回收实现是一种不好的做法,因为它会降低您的代码的可移植性.如果您使用 CPython,可能不会发生资源泄漏,但如果您切换到不使用引用计数的 Python 实现,则需要检查所有代码并确保所有文件都正确关闭.

                  It's bad practice to rely on CPython's garbage collection implementation because it makes your code less portable. You might not have resource leaks if you use CPython, but if you ever switch to a Python implementation which doesn't use reference counting you'll need to go through all your code and make sure all your files are closed properly.

                  您的示例使用:

                  with open("filename") as f:
                       for line in f:
                          # ... do stuff ...
                  

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

                  上一篇:Python + WebDriver -- 使用 unittest 模块时没有启动浏览器 下一篇:Python 垃圾收集器文档

                  相关文章

                  • <bdo id='ORqBz'></bdo><ul id='ORqBz'></ul>

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

                      <legend id='ORqBz'><style id='ORqBz'><dir id='ORqBz'><q id='ORqBz'></q></dir></style></legend>