Python try/except的语法无效
我正在尝试根据文件是否已删除创建异常:Python try/except的语法无效,python,Python,我正在尝试根据文件是否已删除创建异常: def remove_file(): print("Removing the output file: ", output_file) try: os.remove(output_file) except RemoveFileError,e: remove_stat = e.returncode if remove_stat == 0: print("File Removed!
def remove_file():
print("Removing the output file: ", output_file)
try:
os.remove(output_file)
except RemoveFileError,e:
remove_stat = e.returncode
if remove_stat == 0:
print("File Removed!")
但我得到了以下错误:
File ".\aws_ec2_list_instances.py", line 198
except RemoveFileError,e:
^
SyntaxError: invalid syntax
我想我可以随意调用异常。不是这样吗?我如何才能正确地编写它?您使用的是哪种版本的Python?因为Python2和Python3之间的语法发生了变化 在Python2中,它(主要是)
除了Exception、var:
之外,在Python3中它是除了Exception之外的var:
请注意,Python3语法被后传到Python2.6和2.7,因此在这些版本中除了异常之外,将异常视为var是很常见的(如果您希望此特定代码段同时在Python2.6+和3+上工作,这应该是首选的)。您在Python3中使用了Python2语法。→ <代码>除RemoveFileError外为e: