【发布时间】:2019-01-27 01:43:45
【问题描述】:
我正在运行别人的代码:
./run_me.sh
Traceback (most recent call last):
File "train.py", line 13, in <module>
import options
File "/Users/test/Desktop/lang-emerge/options.py", line 44
except IOError, msg: parser.error(str(msg));
^
SyntaxError: invalid syntax
我尝试修改 except 块,但再次出现错误:
./run_me.sh
Traceback (most recent call last):
File "train.py", line 13, in <module>
import options
File "/Users/test/Desktop/lang-emerge/options.py", line 44
except: IOError, msg: parser.error(str(msg));
^
SyntaxError: only single target (not tuple) can be annotated
错误所在的代码:
try: parsed = vars(parser.parse_args());
except: IOError, msg: parser.error(str(msg));
不知道如何解决该错误? msg 是 python try/except 块中的关键字。
修改后的代码,IOError as msg:
我明白了:
./run_me.sh
Traceback (most recent call last):
File "train.py", line 13, in <module>
import options
File "/Users/test/Desktop/lang-emerge/options.py", line 44
except: IOError as msg: parser.error(str(msg));
^
SyntaxError: invalid syntax
【问题讨论】:
-
不,这不是关键字。
except IOError as msg: -
那是旧的 Python 2 语法
-
@coldspeed 谢谢,我尝试使用 IOError 作为 msg,仍然收到错误..
-
@mourinho 没有冒号,不是:
except: IOError as msg:,而是except IOError as msg:。见the documentation -
@PeterWood 谢谢,请把它作为答案,它解决了这个问题!
标签: python python-3.x exception-handling try-catch