首页 > 解决方案 > Try中Python中的语法错误,除了块

问题描述

我正在运行别人的代码:

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

标签: pythonpython-3.xexception-handlingtry-catch

解决方案


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

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

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

推荐阅读