try...except...finally problem in Python 2.5

Steven D'Aprano steve at REMOVEME.cybersource.com.au
Wed Feb 14 21:17:00 EST 2007


On Wed, 14 Feb 2007 18:03:19 -0800, Paul Rubin wrote:

> Steven D'Aprano <steve at REMOVEME.cybersource.com.au> writes:
>> self.isDataLoaded = False
>> try:
>>     f = open(self.filename, 'rb')
>>     f.seek(DATA_OFFSET)
>>     self.__data = f.read(DATA_SIZE)
>>     self.isDataLoaded = True
>> except:
>>     pass
>> else:
>>     pass
>> 
>> (apart from being four lines shorter)
> 
> Your version never closes the file.


Yes it does. Eventually f goes out of scope and is closed automatically.

I don't see where the "with" version closes the file either. How does it
know that I want to call the f's close() method, rather than, say,
f.exit() or f.do_something_else()?



-- 
Steven D'Aprano 




More information about the Python-list mailing list