which one of these is better?

robert no-spam at no-spam-no-spam.com
Thu Oct 26 17:16:31 EDT 2006


Michael B. Trausch wrote:

> Not really über-qualified to say anything, but I think that the
> following would be better:
> 
> try:
> 	f = open('file.sql')
> 	script = f.read()
> 	f.close()
> except IOError:
> 	wx.MessageBox('Message', 'Message Title')
> 
>> Do they both do the same thing?
>>
> 
> Not sure about the with-- I just went to read the PEP on it, and it
> confused me greatly.  :-)  So, I don't know.
> 

when .read() fails the file will not be close here.

to replace the 'with' solution you#d need:


try: 
  f=open(path)
  try: 
      script=f.read()
  finally:
      f.close()
except EnvironmentError,v:
  wx.MessageBox(v, 'Message Title')




-robert



More information about the Python-list mailing list