Opening files without closing them

Erik Max Francis max at alcyone.com
Sun Mar 5 18:59:14 EST 2006


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()

-- 
Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
   Who, my friend, can scale heaven?
   -- _Gilgamesh_, ca. 3rd C. BC



More information about the Python-list mailing list