peo*_*les 1 python shell python-3.x
我的 python 脚本有几个 sys.exit() 调用在某些条件下终止。为了测试它,我打开了一个 python shell 并运行exec(open('script.py').read()). 每当我sys.exit()打电话时,脚本都会终止,python shell 也会终止,这很烦人。
因此,我尝试发挥创意,并尝试通过检查堆栈来确定我的脚本是从命令行运行还是从 shell 运行。来源:检测 python 脚本是从 ipython shell 运行,还是从命令行运行
def exit_now(exit_status):
os.system('pause')
if PythonShell: # how do I return to the python shell ?
???
else: # exit if we are running from the commandline
sys.exit(exit_status)
...
# check if this was run from inside a python shell
frames = len(inspect.stack())
if frames > 1:
PythonShell = True
else:
PythonShell = False
...
if condition1:
exit_now(1)
if condition2:
exit_now(2)
...
exit_now(0)
Run Code Online (Sandbox Code Playgroud)
终止脚本后如何返回到 python shell?
| 归档时间: |
|
| 查看次数: |
1307 次 |
| 最近记录: |