which one of these is better?
Michael B. Trausch
mike$#at^&nospam!%trauschus
Thu Oct 26 14:55:35 EDT 2006
John Salerno wrote:
> def create_sql_script(self):
> try:
> with open('labtables.sql') as sql_script:
> return sql_script.read()
> except IOError:
> wx.MessageBox('Could not locate the file "labtables.sql"',
> 'File Not Found')
>
I can't comment on this, because it won't work in my implemention of
Python, I don't think.
>
> OR
>
>
> def create_sql_script(self):
> try:
> f = open('labtables.sql')
> sql_script = f.read()
> except IOError:
> wx.MessageBox('Could not locate the file "labtables.sql"',
> 'File Not Found')
> finally:
> f.close()
>
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.
-- Mike
More information about the Python-list
mailing list