val*_*rio 5 python exit execution terminate ipython
我正在将脚本运行到 ipython (1.2.1) 中,如果不满足某个条件,我需要它停止。我尝试使用 exit() 语句,但它的行为不符合预期。
以我调用的以下脚本为例test.py:
if(True):
print('Error')
exit()
print('Still here!')
Run Code Online (Sandbox Code Playgroud)
当我使用 运行它时python test.py,我得到:
$python test.py
Error
Run Code Online (Sandbox Code Playgroud)
然后按预期终止执行。
但是,如果我使用从 ipython 运行它run -i test.py,那么我会得到:
In [1]: run -i test.py
Error
Still here!
Run Code Online (Sandbox Code Playgroud)
最后终止 ipython 的执行。问题是,在这种情况下,第二个打印语句仍然被执行,而我需要在遇到 exit() 语句时立即终止脚本的执行。
为什么会发生这种情况,我怎样才能获得我想要的结果?(我正在运行 python 2.7.6)
| 归档时间: |
|
| 查看次数: |
1967 次 |
| 最近记录: |