pythonic way to free resources

Chirayu Krishnappa chirayuk at gmx.net
Tue Aug 20 10:15:02 CEST 2002


I actually meant to write

try:
     try:
     except:
finally:

but wrote a

try:
except:
finally:

I now see that your solution of

try:
     try:
     finally:
except:

works like a charm. Thanks!!

At 06:31 PM 8/20/02 +1200, Greg Ewing wrote:
>That's not legal Python -- you can't have except and finally
>in the same try block. But in any case, you don't have to
>repeat the closes:
>
>   try:
>     try:
>       # stuff that opens f, g
>       # some other processing which may raise an error
>       # stuff that opens h
>     finally:
>       f.close()
>       g.close()
>       h.close()
>   except:
>     # set some flags
>     raise
>
>--
>Greg Ewing, Computer Science Dept,
>University of Canterbury,
>Christchurch, New Zealand
>http://www.cosc.canterbury.ac.nz/~greg





More information about the Python-list mailing list