file.close()

Francois Pinard pinard at iro.umontreal.ca
Thu Jul 24 09:19:53 EDT 2003


[Bryan]

> I'm curious to know how others handle the closing of files.  [...] I'm
> aware that files will automatically be closed when the process exits.

For one, I systematically avoid cluttering my code with unneeded `close'.
The advantages are simplicity and legibility, both utterly important to me.

However, I do understand that if I ever have to move a Python script
to Jython, I will have to revise my scripts for adding the clutter I am
sparing today.  I'm quite accepting to do that revision if this occurs.
Until then, I prefer keeping my scripts as neat as possible.

For me, explicitely closing a file, for which the only reference is about
to disappear through function exiting, would be very similar to using
`del' on any variable I happened to use in that function: gross overkill...

The only reason to call `close' explicitly is when there is a need to close
prematurely.  Absolutely no doubt that such needs exist at times.  But
closing all the time "just in case" is symptomatic of unsure programming.
Or else, it is using Python while still thinking in other languages.

-- 
François Pinard   http://www.iro.umontreal.ca/~pinard





More information about the Python-list mailing list