Opening files without closing them

Robert Kern robert.kern at
Mon Mar 6 01:04:16 CET 2006

Erik Max Francis wrote:
> Robert Kern wrote:
>>>I usually use:
>>>  f = open(file)
>>>  contents =
>>>  f.close()
>>>But now I am wondering if that is the same thing. Which method would
>>>you rather use? Why?
>>Just keep doing what you are doing, please.
> Note quite.  The assignment of the resources to its variable needs to be 
> done before the try:
> 	f = open(file)
> 	try:
> 	    contents =
> 	finally:
> 	    f.close()

Yes, you are correct.

Robert Kern
robert.kern at

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

More information about the Python-list mailing list