try/except/else/finally problem

Peter Otten __peter__ at web.de
Thu Jun 28 13:59:08 EDT 2007


Ed Jensen wrote:

> I'm using:
> 
> Python 2.3.2 (#1, Oct 17 2003, 19:06:15) [C] on sunos5
> 
> 
> And I'm trying to execute:
> 
> #! /usr/bin/env python
> 
> try:
>     f = file('test.txt', 'r')
> except IOError:
>     print 'except'
> else:
>     print 'else'
> finally:
>     print 'finally'
> 
> 
> And the results are:
> 
>   File "./test.py", line 9
>     finally:
>           ^
> SyntaxError: invalid syntax
> 
> 
> What am I doing wrong?

You need Python 2.5 for that to work. In older Python versions you have to
nest try...except...else and try...finally.

Peter





More information about the Python-list mailing list