Search code examples
pythonfilegarbage-collection

Does Python GC close files too?


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

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

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?


Solution

  • UPDATE

    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.

    In the Python 3.8 docs the text has been updated to:

    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.

    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.

    Old Answer:

    Taken from the Python 3.6 docs:

    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 was 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