Opening files without closing them

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


Erik Max Francis wrote:
> Robert Kern wrote:
> 
>>>I usually use:
>>>
>>>try:
>>>  f = open(file)
>>>  contents = f.read()
>>>finally:
>>>  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 = f.read()
> 	finally:
> 	    f.close()

Yes, you are correct.

-- 
Robert Kern
robert.kern at gmail.com

"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