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

      <tfoot id='FduY7'></tfoot>
    1. <small id='FduY7'></small><noframes id='FduY7'>

      1. 对于打开的行(文件名)

        for line in open(filename)(对于打开的行(文件名))
      2. <small id='Oe2jr'></small><noframes id='Oe2jr'>

          <bdo id='Oe2jr'></bdo><ul id='Oe2jr'></ul>
          <legend id='Oe2jr'><style id='Oe2jr'><dir id='Oe2jr'><q id='Oe2jr'></q></dir></style></legend>
        • <tfoot id='Oe2jr'></tfoot>
                <tbody id='Oe2jr'></tbody>

                  <i id='Oe2jr'><tr id='Oe2jr'><dt id='Oe2jr'><q id='Oe2jr'><span id='Oe2jr'><b id='Oe2jr'><form id='Oe2jr'><ins id='Oe2jr'></ins><ul id='Oe2jr'></ul><sub id='Oe2jr'></sub></form><legend id='Oe2jr'></legend><bdo id='Oe2jr'><pre id='Oe2jr'><center id='Oe2jr'></center></pre></bdo></b><th id='Oe2jr'></th></span></q></dt></tr></i><div id='Oe2jr'><tfoot id='Oe2jr'></tfoot><dl id='Oe2jr'><fieldset id='Oe2jr'></fieldset></dl></div>
                  本文介绍了对于打开的行(文件名)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我经常看到类似于

                  for line in open(filename):
                      do_something(line)
                  

                  什么时候文件名会被这段代码关闭?

                  When does filename get closed with this code?

                  写出来会更好

                  with open(filename) as f:
                      for line in f.readlines():
                          do_something(line)
                  

                  推荐答案

                  filename 超出范围时会被关闭.这通常是方法的结束.

                  filename would be closed when it falls out of scope. That normally would be the end of the method.

                  是的,最好使用 with.

                  一旦你有了一个文件对象,你就可以通过调用这个对象的方法来执行所有的文件 I/O.[...] 完成文件后,您应该通过在对象上调用 close 方法来关闭与文件的连接:

                  Once you have a file object, you perform all file I/O by calling methods of this object. [...] When you are done with the file, you should finish by calling the close method on the object, to close the connection to the file:

                  input.close()
                  

                  在简短的脚本中,人们经常省略这一步,因为 Python 在垃圾回收期间回收文件对象时会自动关闭文件(这在主流 Python 中意味着文件几乎立即关闭,尽管其他重要的 Python 实现,例如如 Jython 和 IronPython,有其他更宽松的垃圾收集策略).尽管如此,尽快关闭文件是一种很好的编程习惯,在较大的程序中尤其是一个好主意,否则可能会有更多无用的打开文件躺在那里的风险.请注意,try/finally 特别适合确保关闭文件,即使函数因未捕获的异常而终止.

                  In short scripts, people often omit this step, as Python automatically closes the file when a file object is reclaimed during garbage collection (which in mainstream Python means the file is closed just about at once, although other important Python implementations, such as Jython and IronPython, have other, more relaxed garbage collection strategies). Nevertheless, it is good programming practice to close your files as soon as possible, and it is especially a good idea in larger programs, which otherwise may be at more risk of having excessive numbers of uselessly open files lying about. Note that try/finally is particularly well suited to ensuing that a file gets closed, even when a function terminates due to an uncaught exception.

                  Python Cookbook,第 59 页.

                  这篇关于对于打开的行(文件名)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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% 就足够了)

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

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

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