[Python-ideas] Extending error handling on with statements.

Greg Ewing greg.ewing at canterbury.ac.nz
Mon Mar 28 07:11:30 CEST 2011


Jakob Bowyer wrote:

> try:
>     with open('nofile.txt','r') as inp:
>         #nofile.txt does not exist and throws an exception
> except IOError:
>     with open('another.txt','r') as inp:
>         #carry on where you left off...

You could write this as

   try:
     inp = open('nofile.txt','r')
   except IOError:
     inp = open('another.txt','r')
   with inp:
     ...

-- 
Greg



More information about the Python-ideas mailing list