Does Python GC close files too?

后端 未结 2 1855
长情又很酷
长情又很酷 2020-12-07 05:07

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

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

I assume that since the o

相关标签:
2条回答
  • 2020-12-07 05:32

    Taken from the docs (python 3.6):

    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.

    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 will be clsoed once the with block ends

    In the python 2.7 docs, the wording is different:

    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.

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

    0 讨论(0)
  • 2020-12-07 05:37

    It depends on what you do, check out this description how it works.

    In general I would recommend to use the context manager of the file:

    with open("foo", "r") as f:
        for line in f.readlines():
        # ....
    

    which is similar to (for basic understanding):

    file_context_manager = open("foo", "r").__enter__()
    for line in file_context_manager.readlines():
        # ....
    file_context_manager.__exit__()
    

    The first version is a lot more readable, and the withstatement calls the exit method automatically (plus a bit more context handling). The file will be closed automatically when the scope of the with statement is left.

    0 讨论(0)
提交回复
热议问题