File not closed on exception
gagsl-py2 at yahoo.com.ar
Tue Oct 20 21:13:35 CEST 2009
En Tue, 20 Oct 2009 04:47:02 -0300, arve.knudsen at gmail.com
<arve.knudsen at gmail.com> escribió:
> On 20 Okt, 09:40, "Gabriel Genellina" <gagsl-... at yahoo.com.ar> wrote:
>> En Tue, 20 Oct 2009 03:23:49 -0300, arve.knud... at gmail.com
>> <arve.knud... at gmail.com> escribió:
>> > I agree, but like I said, I've been told that this (implicit closing
>> > of files) is the correct style by more merited Python developers, so
>> > that made me think I was probably wrong ..
>> Then tell those "more merited Python developers" that they're wrong,
>> and that the right way to ensure a file is closed when you're done with
>> it is to use a `with` statement (or a try/finally block in old Python
> Easier said than done :) In any case, I now have this discussion as a
> useful reference in the future. Thanks!
If this thread is not enough, you can ask them to read the official Python
"It is good practice to use the with keyword when dealing with file
objects. This has the advantage that the file is properly closed after its
suite finishes, even if an exception is raised on the way. It is also much
shorter than writing equivalent try-finally blocks."
More information about the Python-list