Python code to restart program

the living tribunal

Moderator
Staff member
moderator
Python:
import os
import sys

def restart_program():
    python = sys.executable
    os.execl(python, python, *sys.argv)

# Example usage
print("Program is running...")
restart_program()
 

the living tribunal

Moderator
Staff member
moderator
Code to edit program code
Python:
import os
import sys

def modify_code():
    with open(__file__, 'r') as file:
        lines = file.readlines()

    # Example modification: Add a print statement
    lines.insert(-1, 'print("Code modified and restarted!")\n')

    with open(__file__, 'w') as file:
        file.writelines(lines)

def restart_program():
    python = sys.executable
    os.execl(python, python, *sys.argv)

# Example usage
print("Program is running...")
modify_code()
restart_program()
 
Top