[Tutor] "=" invalid syntax ?

Daniel Ehrenberg littledanehren at yahoo.com
Sun Feb 1 21:00:27 EST 2004


> try/except and try/finally are two different forms. 
> There is no try/except/finally.
> 
> (thanks to Alex Martelli) it should look more like:
> 
> try:
>      try:
>          fi = open('test.py', 'r')
>      except IOError:
>          print "Can't open file for reading."
>      else:
>          for line in fi:
>              print line
> finally:
>      fi.close()
> 
> The else is a convenient to limit the scope of the
> try statement.
> 
> The exceptions chapter of "Python in a Nutshell" has
> very lucid descriptions
> of exception handling strategies.

I'm probably making some big mistake again, but if
you're using both except and else, why not just write:

try:
    fi = open('test.py', 'r')
except IOError:
    print "Can't open file for reading."
else:
    for line in fi:
        print line

fi.close()

Daniel Ehrenberg

__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free web site building tool. Try it!
http://webhosting.yahoo.com/ps/sb/



More information about the Tutor mailing list