尝试中的Python语法错误,除块外

时间:2018-08-20 22:00:42

标签: python python-3.x exception-handling try-catch

我正在运行别人的代码:

./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

1 个答案:

答案 0 :(得分:1)

except: IOError as msg: parser.error(str(msg));

您有不必要的冒号。您应该删除它:下面的代码是正确的代码。

except IOError as msg: parser.error(str(msg));