Python restart program
Question:
I made a program that asks you at the end for a restart.
I import os
and used os.execl(sys.executable, sys.executable, * sys.argv)
but nothing happened, why?
Here’s the code:
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)
Answers:
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 program will be closed...")
sys.exit(0)
os.execl(path, arg0, arg1, …)
sys.executable
: python executeable
os.path.abspath(__file__)
: the python code file you are running.
*sys.argv
: remaining argument
It will execute the program again like python XX.py arg1 arg2
.
os.execv(sys.executable, ['python'] + sys.argv)
solved the problem.
Maybe os.execv will work but why not use directly using os.system('python "filename.py"')
if you have environment and path variable set something like :
import os
print("Hello World!")
result=input("nDo you want to restart the program? [y/n] > ")
if result=='y':
os.system('python "C:/Users/Desktop/PYTHON BEST/Hackerrank.py"')
else:
print("nThe program will be closed...")
Just import the program you want to restart and run it under the desired condition like this
Title: hello. py
import hello
if (enter generic condition here):
hello
try using;
while True:
answer = input("nDo you want to restart the program? [y/n] > ")
if answer == "n":
print("nOk, Bye!")
break
or
retry = True
while retry:
answer = input("nDo you want to restart the program? [y/n] > ")
if answer == "n":
print("nOk, Bye!")
retry = False
It is a way that you can easily modify to your liking, and it also means that you can load variables once instead of loading them every time you restart. This is just a way to do it without any libraries. You indent all your code and put it in a while loop, and that while loop determines whether your code restarts or not. To exit it, you put in a break or change a variable. It is easiest because you don’t have to understand a new library.
I made a program that asks you at the end for a restart.
I import os
and used os.execl(sys.executable, sys.executable, * sys.argv)
but nothing happened, why?
Here’s the code:
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 program will be closed...")
sys.exit(0)
os.execl(path, arg0, arg1, …)
sys.executable
: python executeable
os.path.abspath(__file__)
: the python code file you are running.
*sys.argv
: remaining argument
It will execute the program again like python XX.py arg1 arg2
.
os.execv(sys.executable, ['python'] + sys.argv)
solved the problem.
Maybe os.execv will work but why not use directly using os.system('python "filename.py"')
if you have environment and path variable set something like :
import os
print("Hello World!")
result=input("nDo you want to restart the program? [y/n] > ")
if result=='y':
os.system('python "C:/Users/Desktop/PYTHON BEST/Hackerrank.py"')
else:
print("nThe program will be closed...")
Just import the program you want to restart and run it under the desired condition like this
Title: hello. py
import hello
if (enter generic condition here):
hello
try using;
while True:
answer = input("nDo you want to restart the program? [y/n] > ")
if answer == "n":
print("nOk, Bye!")
break
or
retry = True
while retry:
answer = input("nDo you want to restart the program? [y/n] > ")
if answer == "n":
print("nOk, Bye!")
retry = False
It is a way that you can easily modify to your liking, and it also means that you can load variables once instead of loading them every time you restart. This is just a way to do it without any libraries. You indent all your code and put it in a while loop, and that while loop determines whether your code restarts or not. To exit it, you put in a break or change a variable. It is easiest because you don’t have to understand a new library.