File not closed on exception

arve.knudsen at gmail.com arve.knudsen at gmail.com
Tue Oct 20 09:47:02 CEST 2009


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ó:
>
>
>
>
>
> > On Oct 19, 5:56 pm, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
> > wrote:
> >> En Mon, 19 Oct 2009 09:45:49 -0200, arve.knud... at gmail.com  
> >> <arve.knud... at gmail.com> escribió:
>
> >> > I thought that file objects were supposed to be garbage-collected and
> >> > automatically closed once they go out of scope, at least that's what
> >> > I've been told by more merited Python programmers.
>
> >> When an object holds references to external resources that must be  
> >> freed,  
> >> this is not a good idea. Being explicit with the resource deallocation  
> >> is  
> >> much better than relying on object destruction sometime in the future...
>
> > 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 releases)

Easier said than done :) In any case, I now have this discussion as a
useful reference in the future. Thanks!

Arve



More information about the Python-list mailing list