Will file be closed automatically in a "for ... in open..." statement?

Chris Angelico rosuav at gmail.com
Tue Feb 16 04:16:01 EST 2016


On Tue, Feb 16, 2016 at 7:39 PM,  <jfong at ms4.hinet.net> wrote:
> I know
>
>     with open('foo.txt') as f:
>         ...do something...
>
> will close the file automatically when the "with" block ends.
>
> I also saw codes in a book:
>
>     for line in open('foo.txt'):
>         ...do something...
>
> but it didn't mention if the file will be closed automatically or not when the "for" block ends. Is there any document talking about this? and how to know if a file is in "open" or not?
>

The file will be closed when the open file object is disposed of. That
will happen at some point after there are no more references to it.
You're guaranteed that it stays around for the entire duration of the
'for' loop (the loop keeps track of the thing it's iterating over),
but exactly when after that is not guaranteed. In current versions of
CPython, the garbage collector counts references, so the file will be
closed immediately; but other Python interpreters, and future versions
of CPython, may not behave the same way. So the file will *probably*
be closed *reasonably* promptly, but unlike the "with" case, you have
no guarantee that it'll be immediate.

For small scripts, it probably won't even matter, though. You're
unlikely to run out of file handles, and the only time it would matter
is if you're opening, closing, and then reopening the file - for
example:

fn = input("Name a file to frobnosticate: ")
with open(fn) as f:
    data = []
    for line in f:
        data = frobnosticate(data, line)
with open(fn, "w") as f:
    f.writelines(data)

For this to work reliably, the file MUST be closed for reading before
it's opened for writing. The context managers are important. But this
is pretty unusual.

Of course, since it's so little trouble to use the 'with' block, it's
generally worth just using it everywhere. Why run the risk? :)

ChrisA
who often forgets to use 'with' anyway


More information about the Python-list mailing list