Is a with on open always necessary?

Andrea Crotti andrea.crotti.0 at gmail.com
Fri Jan 20 10:44:17 EST 2012


I normally didn't bother too much when reading from files, and for example
I always did a

content = open(filename).readlines()

But now I have the doubt that it's not a good idea, does the file 
handler stays
open until the interpreter quits?

So maybe doing a

with open(filename) as f:
      contents = f.readlines()

is always a better idea??



More information about the Python-list mailing list