<tfoot id='95bjm'></tfoot>

    <small id='95bjm'></small><noframes id='95bjm'>

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

      python如何关闭已被gc'ed的文件?

      How does python close files that have been gc#39;ed?(python如何关闭已被gced的文件?)
        1. <i id='Htfp7'><tr id='Htfp7'><dt id='Htfp7'><q id='Htfp7'><span id='Htfp7'><b id='Htfp7'><form id='Htfp7'><ins id='Htfp7'></ins><ul id='Htfp7'></ul><sub id='Htfp7'></sub></form><legend id='Htfp7'></legend><bdo id='Htfp7'><pre id='Htfp7'><center id='Htfp7'></center></pre></bdo></b><th id='Htfp7'></th></span></q></dt></tr></i><div id='Htfp7'><tfoot id='Htfp7'></tfoot><dl id='Htfp7'><fieldset id='Htfp7'></fieldset></dl></div>

            <legend id='Htfp7'><style id='Htfp7'><dir id='Htfp7'><q id='Htfp7'></q></dir></style></legend>
              <bdo id='Htfp7'></bdo><ul id='Htfp7'></ul>
              • <small id='Htfp7'></small><noframes id='Htfp7'>

                  <tbody id='Htfp7'></tbody>
                <tfoot id='Htfp7'></tfoot>
              • 本文介绍了python如何关闭已被gc'ed的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我一直认为如果文件打开而不关闭就会泄漏,但我只是验证了如果我输入以下代码行,文件就会关闭:

                I had always assumed that a file would leak if it was opened without being closed, but I just verified that if I enter the following lines of code, the file will close:

                >>> f = open('somefile.txt')
                >>> del f
                

                纯粹出于好奇,这是如何工作的?我注意到该文件不包含 __del__ 方法.

                Just out of sheer curiosity, how does this work? I notice that file doesn't include a __del__ method.

                推荐答案

                至少在 CPython 中,文件对象在释放时会关闭.请参阅 CPython 源代码中 Objects/fileobject.c 中的 file_dealloc 函数.Dealloc 方法有点像 C 类型的 __del__,除了没有 __del__ 固有的一些问题.

                In CPython, at least, files are closed when the file object is deallocated. See the file_dealloc function in Objects/fileobject.c in the CPython source. Dealloc methods are sort-of like __del__ for C types, except without some of the problems inherent to __del__.

                这篇关于python如何关闭已被gc'ed的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                env: python: No such file or directory(env: python: 没有这样的文件或目录)
                How to evaluate environment variables into a string in Python?(如何在 Python 中将环境变量评估为字符串?)
                Python - temporarily modify the current process#39;s environment(Python - 临时修改当前进程的环境)
                Change current process environment#39;s LD_LIBRARY_PATH(更改当前进程环境的 LD_LIBRARY_PATH)
                Reading and writing environment variables in Python?(在 Python 中读写环境变量?)
                When to use sys.path.append and when modifying %PYTHONPATH% is enough(何时使用 sys.path.append 以及何时修改 %PYTHONPATH% 就足够了)
                  <bdo id='MHAKK'></bdo><ul id='MHAKK'></ul>
                    <i id='MHAKK'><tr id='MHAKK'><dt id='MHAKK'><q id='MHAKK'><span id='MHAKK'><b id='MHAKK'><form id='MHAKK'><ins id='MHAKK'></ins><ul id='MHAKK'></ul><sub id='MHAKK'></sub></form><legend id='MHAKK'></legend><bdo id='MHAKK'><pre id='MHAKK'><center id='MHAKK'></center></pre></bdo></b><th id='MHAKK'></th></span></q></dt></tr></i><div id='MHAKK'><tfoot id='MHAKK'></tfoot><dl id='MHAKK'><fieldset id='MHAKK'></fieldset></dl></div>
                      <tbody id='MHAKK'></tbody>

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

                          <tfoot id='MHAKK'></tfoot>

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