[英]How do I write a script that re-execute a python script after it exits back to cmd prompt?
[英]How do I drop back to the python shell after exiting a script?
我的python脚本有几个sys.exit()调用,可以在某些条件下终止。 为了测试它,我打开了一个python shell并运行exec(open('script.py').read())
。 每当我点击sys.exit()
调用时,脚本就会终止,而python外壳也会终止,这很sys.exit()
。
因此,我尝试发挥创意,并尝试通过检查堆栈来确定我的脚本是从命令行运行还是从外壳运行。 来源: 检测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)
终止脚本后如何退回到python shell?
python -i <script>
“ i”用于互动。
怎么样:
try:
exec(open('script.py').read())
except SystemExit:
pass
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.