![](/img/trans.png)
[英]Send output of Python program that receives keyboard input to terminal and to file
[英]Send terminal input data when restarting a program (python)
我的脚本首先要求终端输入:
ans = raw_input("Do thing A (1) / Do thing B (2)")
然后运行代码,并在出现异常时重新启动自身。
def restart_program():
python = sys.executable
os.execl(python, python, * sys.argv)
问题是我需要人工再次输入该选项,所以我尝试了此操作:
def restart_program():
python = sys.executable
os.execl(python, python, '1', * sys.argv)
但这没有用。 重新启动时如何发送选项?
像注释中提到的其他脚本一样,尝试使用while循环,smthng这样的shell脚本通过shell脚本运行python脚本(首先运行wo cmd行参数):
arg=''
while true
do
python restart.py $arg
arg='1'
sleep 1800
done
并在您的python代码中检查是否提供了参数:
try:
ans = sys.argv[1]
except IndexError:
ans = raw_input("Do thing A (1) / Do thing B (2)")
您只需要在处理异常后退出程序即可,而不必从代码中重新启动它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.