5

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)
Arszilla
  • 159
  • 1
  • 2
  • 12
claudio26
  • 149
  • 1
  • 1
  • 8

5 Answers5

6
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.

Milos Ivanovic
  • 592
  • 3
  • 9
  • 21
SangminKim
  • 8,358
  • 14
  • 69
  • 125
4

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...")
DARK_C0D3R
  • 2,075
  • 16
  • 21
0

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
Kraigolas
  • 5,121
  • 3
  • 12
  • 37
Falcon
  • 9
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 29 '21 at 03:32
0

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.

Tsunami014
  • 17
  • 5
-1
os.execv(sys.executable, ['python'] + sys.argv)

solved the problem.

claudio26
  • 149
  • 1
  • 1
  • 8
  • This code seems to be the most common way to restart a python program. I'm guessing you're being down voted because of the lack of explanation. Note that, os.execv is not really a good option on Windows. https://bugs.python.org/issue19124 – ReenigneArcher Mar 26 '22 at 23:40