[Python-ideas] with statement vs. try...except...finally
Scott David Daniels
Scott.Daniels at Acm.Org
Sat May 30 00:25:44 CEST 2009
Gerald Britton wrote:
> I'm wondering if the "with" statement should have exception clauses
> like the "try" statement, even though this seems to defeat part of the
> reason for the "with" statement....
> something like:
> try:
> f = open('foo')
> line = f.readline()
> f.close()
> except IOError:
> line = 'default'
> ... Fine so far, but what if I want to be more granular? ...
> I can't see how to replace the above try-triplet with a "with"
> encapsulation. Or, do I have to wrap the "with" statement in try like
> this:
How about using the error information?:
try:
with open('pov_export/.hg/requires') as src:
line = src.read(30)
except IOError, why:
line = 'Problem: %s' % why
--Scott David Daniels
Scott.Daniels at Acm.Org
More information about the Python-ideas
mailing list