我正在尝试根据文件是否已被删除来创建一个异常:
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我想我可以随心所欲地给这个异常命名。难道不是这样吗?我怎样才能正确地写出这段代码呢?
发布于 2019-03-21 23:45:13
您使用的是哪个版本的Python?因为Python2和Python3之间的语法发生了变化。
在Python2中,它(主要)是except Exception, var:,而在Python3中是except Exception as var:。
请注意,Python3语法已向后移植到Python2.6和2.7,因此在这些版本中看到except Exception as var并不少见(如果您想让这段特定的代码同时在Python 2.6+和3+上工作,这应该是首选)。
https://stackoverflow.com/questions/55284217
复制相似问题