Q on explicitly calling file.close

kj no.email at please.post
Sat Sep 5 12:14:02 EDT 2009





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



More information about the Python-list mailing list