Q on explicitly calling file.close

MRAB python at mrabarnett.plus.com
Sat Sep 5 19:01:59 CEST 2009


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?
> 
CPython uses reference counting, so an object is garbage collected as
soon as there are no references to it, but that's just an implementation
detail.

Other implementations, such as Jython and IronPython, don't use
reference counting, so you don't know when an object will be garbage
collected, which means that the file might remain open for an unknown
time afterwards in case 1 above.

Most people use CPython, so it's not surprising that case 1 is so
common.



More information about the Python-list mailing list