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

Mark Lawrence breamoreboy at yahoo.co.uk
Wed Feb 17 21:05:07 EST 2016

On 18/02/2016 01:29, jfong at ms4.hinet.net wrote:
> The "for ... open ..." is definitely not a good design pattern. It opens a file at "for" block but leaves it closed somewhere in the sky.

Hardly, as all ready explained, but how about this

handle = open('foo.txt')
for line in handle :
     ...do something...

Is that better?

My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

More information about the Python-list mailing list