【发布时间】:2020-05-01 19:47:53
【问题描述】:
我有一个 bash 脚本来启动我的 python 脚本。重点是,我将很多(有时是变化的)参数交给了 python 脚本。所以我发现使用 bash 脚本启动我的 python 脚本很有用,我在其中“保存”我的参数列表。
#!/bin/bash
cd $(dirname $0)
python3 script.py [arg0] [arg1]
在我的 Python 脚本中,我实现了 KeyboardInterrupt-Exception,它将保存一些打开的文件,然后退出 Python 脚本。 现在我的问题是:当我运行 shell 脚本时,我必须至少按 3 次 Strg+C,得到一些 python 错误并且它会停止。
我猜对了吗,我的 Strg+C 不被 python 识别,而是被 shell 脚本识别? 有没有办法将键盘中断从 shell 脚本交给运行在其中的 python 脚本?
顺便说一句:python 脚本正在运行一个无限循环(如果这很重要)
Python 脚本的异常如下所示。正如所指出的,它运行一个无限循环。
while True:
try:
#doing stuff here
except KeyboardInterrupt:
for file in files:
file.close()
break
【问题讨论】:
-
请提供处理异常代码
标签: python python-3.x bash shell