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

jfong at ms4.hinet.net jfong at ms4.hinet.net
Tue Feb 16 03:39:22 EST 2016

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?

--Jach Fong

More information about the Python-list mailing list