This snippet defines a function restart_program() which restarts your python program from within your python program.
Restart your python program.
Pupo commented: Nice! +5
import sys
import os
def restart_program():
"""Restarts the current program.
Note: this function does not return. Any cleanup action (like
saving data) must be done before calling this function."""
python = sys.executable
os.execl(python, python, * sys.argv)
if __name__ == "__main__":
answer = raw_input("Do you want to restart this program ? ")
if answer.lower().strip() in "y yes".split():
restart_program()
Stefano Mtangoo 455 Senior Poster
Boubakr 0 Newbie Poster
Edwin Okli 6 Newbie Poster
Gribouillis 1,391 Programming Explorer Team Colleague
Edwin Okli 6 Newbie Poster
Gribouillis 1,391 Programming Explorer Team Colleague
Edwin Okli 6 Newbie Poster
TrustyTony 888 pyMod Team Colleague Featured Poster
Aoradon 0 Newbie Poster
Louis_2 14 Newbie Poster
Gribouillis 1,391 Programming Explorer Team Colleague
Louis_2 14 Newbie Poster
Gribouillis 1,391 Programming Explorer Team Colleague
Louis_2 commented: Thanks for that, I'm now using a while loop instead. +0
Gribouillis 1,391 Programming Explorer Team Colleague
Aman_7 0 Newbie Poster
Gribouillis 1,391 Programming Explorer Team Colleague
Aman_7 0 Newbie Poster
Gribouillis 1,391 Programming Explorer Team Colleague
Claire_2 0 Newbie Poster
Gribouillis 1,391 Programming Explorer Team Colleague
Aman_7 0 Newbie Poster
Gribouillis 1,391 Programming Explorer Team Colleague
S._1 0 Newbie Poster
Gribouillis 1,391 Programming Explorer Team Colleague
S._1 0 Newbie Poster
Gribouillis 1,391 Programming Explorer Team Colleague
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.