This question already has an answer here:
这个问题在这里已有答案:
- Python try…except comma vs 'as' in except 4 answers
- Python尝试...除了逗号vs'as',除了4个答案
I've got a SyntaxError on my except:
我的上面有一个SyntaxError,除了:
try:
opts, args = getopt.getopt(sys.argv[1:], 'P:D:H:d:u:p:nvhmJi:c:Ml:TB:',
['host=', 'port=', 'directory=', 'user=', 'password=',
'daemon=', 'noauth', 'help', 'verbose', 'mysql',
'icounter=', 'config=', 'nolock', 'nomime', 'loglevel', 'noiter',
'baseurl='])
except getopt.GetoptError, e:
print usage
print '>>>> ERROR: %s' % str(e)
sys.exit(2)
I get the error:
我收到错误:
File "main.py", line 199
except getopt.GetoptError, e:
SyntaxError: invalid syntax
Anyone have any idea?
有人有什么想法吗?
2 个解决方案
#1
33
You use python3 and in python3 the raise syntax no longer accepts comma-separated arguments.
你使用python3,在python3中,raise语法不再接受以逗号分隔的参数。
Use as
instead:
用作代替:
except getopt.GetoptError as e:
This form is also backwards-compatible with 2.6 and 2.7.
此表单也向后兼容2.6和2.7。
#2
5
Your syntax is invalid for catching the exception
您的语法对于捕获异常无效
You should have written except getopt.GetoptError as e:
instead of except getopt.GetoptError, e:
您应该编写除了getopt.GetoptError之外的e:而不是getopt.GetoptError,e:
#1
33
You use python3 and in python3 the raise syntax no longer accepts comma-separated arguments.
你使用python3,在python3中,raise语法不再接受以逗号分隔的参数。
Use as
instead:
用作代替:
except getopt.GetoptError as e:
This form is also backwards-compatible with 2.6 and 2.7.
此表单也向后兼容2.6和2.7。
#2
5
Your syntax is invalid for catching the exception
您的语法对于捕获异常无效
You should have written except getopt.GetoptError as e:
instead of except getopt.GetoptError, e:
您应该编写除了getopt.GetoptError之外的e:而不是getopt.GetoptError,e: