Why no open(f, "w").write()?

Gary Duzan gary at duzan.org
Thu May 30 13:30:18 EDT 2002


In article <mailman.1022720450.24884.python-list at python.org>,
François Pinard <pinard at iro.umontreal.ca> wrote:
=>[Sean 'Shaleh' Perry]
=>
=>> As a matter of habit in any long running program I am always close() what
=>> needs to be closed and in other way attempt to help the garbage collector.
=>
=>This is surely good to explicitly `close()' when one is done with a file,
=>but needs to keep a reference on this file for other reasons.  The nicest
=>is not keeping a reference to the file, whenever it can be avoided easily.

   Actually, after a quick read of the spec I don't see any guarantee
that a file object will be closed when it is destroyed, so yes, an
explicit close() should always be used.

   Of course, if write() returned the file object instead of nothing,
one could use something like:

	open(f, "w").write(data).close()

but that might not be Pythonic enough (or too C++-y) for some.

					Gary Duzan
					BBN Technologies
					A Verizon Company





More information about the Python-list mailing list