Q on explicitly calling file.close

r rt8396 at gmail.com
Sat Sep 5 20:54:07 CEST 2009


On Sep 5, 1:17 pm, Dave Angel <da... at ieee.org> wrote:
> kj wrote:
> > There's something wonderfully clear about code like this:
>
> >     # (1)
> >     def spam(filename):
> >         for line in file(filename):
> >             do_something_with(line)
>
> > It is indeed pseudo-codely beautiful.  But I gather that it is not
> > correct to do this, and that instead one should do something like
>
> >     # (2)
> >     def spam(filename):
> >         fh = file(filename)
> >         try:
> >             for line in fh:
> >                 do_something_with(line)
> >         finally:
> >             fh.close()
>
> > ...or alternatively, if the with-statement is available:
>
> >     # (3)
> >     def spam(filename):
> >         with file(filename) as fh:
> >             for line in fh:
> >                 do_something_with(line)
>
> > Mind you, (3) is almost as simple as (1) (only one additional line),
> > but somehow it lacks (1)'s direct simplicity.  (And it adds one
> > more indentation level, which I find annoying.)  Furthermore, I
> > don't recall ever coming across either (2) or (3) "in the wild",
> > even after reading a lot of high-quality Python code (e.g. standard
> > library modules).
>
> > Finally, I was under the impression that Python closed filehandles
> > automatically when they were garbage-collected.  (In fact (3)
> > suggests as much, since it does not include an implicit call to
> > fh.close.) If so, the difference between (1) and (3) does not seem
> > very big.  What am I missing here?
>
> > kynn
>
> We have to distinguish between reference counted and garbage collected.  
> As MRAB says, when the reference count goes to zero, the file is
> immediately closed, in CPython implementation.  So all three are
> equivalent on that platform.
>
> But if you're not sure the code will run on CPython, then you have to
> have something that explicitly catches the out-of-scopeness of the file
> object.  Both your (2) and (3) do that, with different syntaxes.
>
> DaveA

Stop being lazy and close the file. You don't want open file objects
just floating around in memory. Even the docs says something like
"yes, python will free the memory associated with a file object but
you can never *really* be sure *when* this will happen, so just
explicitly close the damn thing!". Besides, you can't guarantee that
any data has been written without calling f.flush() or f.close()
first. What if your program crashes and no data is written? huh?

I guess i could put my pants on by jumping into both legs at the same
time thereby saving one step, but i my fall down and break my arm. I
would much rather just use the one leg at a time approach...



More information about the Python-list mailing list