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

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -