python - Good way of closing a file -
let say, have following code:
from sys import exit def parseline(l): if '#' not in l: print 'invalid expresseion' exit(1) return l open('somefile.txt') f: l in f: print parseline(l) (note demo code. actual program more complex.)
now, how know if have safely closed open files when exit program? @ point assuming files have been closed. programs working ok, want them robust , free of problems related files not closed properly.
one of chief benefits of with block files automatically close file, if there's exception.
https://docs.python.org/2/tutorial/inputoutput.html#methods-of-file-objects
Comments
Post a Comment