I frequently see python code similar to
for line in open(filename): do_something(line)
When does filename get closed with this code?
Would it be better to write
with open(filename) as f: for line in f.readlines(): do_something(line)
The with part is better because it close the file afterwards. You don't even have to use readlines(). for line in file is enough.
with
readlines()
for line in file
I don't think the first one closes it.