我想知道python是否有错误报告消息相当于$!在perl?任何能给我答案的人都将不胜感激.
添加:
example% ./test
File "./test", line 7
test1 = test.Test(dir)
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
当Exception发生时,我得到了类似的东西.如果我应用try和catch块,我可以捕获它并使用sys.exit(message)来记录消息.但是,我是否有机会获得字符串SyntaxError:无效语法并将其放入消息中
Python通常使用异常来报告错误.如果某些OS操作返回错误代码,则会引发您在try-except块中捕获的异常.对于OS操作,即OSError.errno包含在异常实例中.
from __future__ import print_function
import os
try:
os.stat("xxx")
except OSError as err:
print (err)
# The "err" object is on instance of OSError. It supports indexing, with the first element as the errno value.
print(err[0])
Run Code Online (Sandbox Code Playgroud)
输出:
[Errno 2] No such file or directory: 'xxx'
2
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
221 次 |
| 最近记录: |