Python重启程序 [英] Python restart program
本文介绍了Python重启程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我制作了一个程序,最后要求您重启.
I made a program that asks you at the end for a restart.
我import os
并使用了os.execl(sys.executable, sys.executable, * sys.argv)
但是什么也没发生,为什么?
I import os
and used os.execl(sys.executable, sys.executable, * sys.argv)
but nothing happened, why?
代码如下:
restart = input("\nDo you want to restart the program? [y/n] > ")
if str(restart) == str("y"):
os.execl(sys.executable, sys.executable, * sys.argv) # Nothing hapens
else:
print("\nThe program will be closed...")
sys.exit(0)
推荐答案
import os
import sys
restart = input("\nDo you want to restart the program? [y/n] > ")
if restart == "y":
os.execl(sys.executable, os.path.abspath(__file__), *sys.argv)
else:
print("\nThe programm will me closed...")
sys.exit(0)
os.execl(路径,arg0,arg1,...)
os.execl(path, arg0, arg1, ...)
sys.executable
:python可执行
sys.executable
: python executeable
os.path.abspath(__file__)
:您正在运行的python代码文件.
os.path.abspath(__file__)
: the python code file you are running.
*sys.argv
:剩余参数
它将再次执行程序,类似于python XX.py arg1 arg2
.
It will execute the program again something like python XX.py arg1 arg2
.
这篇关于Python重启程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文