catching an IOError in python
I wrote a method that does some stuff and catches bad filenames. what should happen is if the path doesn't exist, it throws an IOError. however, it thinks my exception handling is bad syntax... why??
def whatever(): try: # do stuff # and more stuff except IOError: # do this pass whatever()
but before it even gets to calling whatever()
, it prints the following:
Traceback (most recent call last): File "", line 1, in File "getquizzed.py", line 55 except IOError: ^ SyntaxError: invalid syntax
when imported...help?!
Check your indenting. This unhelpful SyntaxError
error has fooled me before. :)
只是在你的try
块中缺少某些东西,例如pass
或其他东西,否则会产生缩进错误。
there's 1 more possible if you're privileged to have an older installation
and
you're using the 'as' syntax:
except IOError as ioe:
and
the parser's getting tripped up on 'as'.
Using as
is the preferred syntax in Python 2.6 and better.
It's a syntax error in Python 2.5 and older. For pre-2.6, use this:
except IOError, ioe:
上一篇: 如何在Python中更改目录(cd)?
下一篇: 在Python中捕获IOError