No need to close file?
Kevin Watters
kevin at dotsyntax.com
Tue Jul 18 21:48:51 EDT 2006
There's always the new 'with' statement in Python 2.5. So instead of
> f = open('foo', 'r')
> try:
> for line in f:
> print line
> finally:
> f.close()
>
...you do:
with open('foo','r') as f:
for line in f:
print line
It's at least a little bit cleaner, and it will close the file if there's an
exception as well.
(See http://docs.python.org/dev/whatsnew/pep-343.html and don't forget to include
from __future__ import with_statement
at the top of the file)
More information about the Python-list
mailing list