Python 除了我的有什么问题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14908789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:49:30  来源:igfitidea点击:

What's wrong with my except?

pythonexception-handlingsyntax-errorexcept

提问by lagarkane

I've got a SyntaxError on my except:

我有一个语法错误,除了:

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?

任何人有任何想法?

回答by Abhijit

Your syntax is invalid for catching the exception

您的语法对于捕获异常无效

You should have written except getopt.GetoptError as e:instead of except getopt.GetoptError, e:

你应该写except getopt.GetoptError as e:而不是except getopt.GetoptError, e:

回答by georg

You use python3 and in python3 the raise syntax no longer accepts comma-separated arguments.

您使用 python3 并且在 python3中 raise 语法不再接受逗号分隔的参数

Use asinstead:

使用as来代替:

except getopt.GetoptError as e:

This form is also backwards-compatible with 2.6 and 2.7.

这种形式也向后兼容 2.6 和 2.7。