Restart python-script from within itself

前端 未结 9 1866
谎友^
谎友^ 2020-12-02 15:27

I have a python-based GTK application that loads several modules. It is run from the (linux) terminal like so:

./myscript.py --some-flag setting

9条回答
  •  失恋的感觉
    2020-12-02 16:07

    I'm using this to give an option for the users to restart the script within the console. Hope it could be a help.

    def close_restart(self,csvfile):
    
        choice = input('Do you want to restart the program? Please select \'Y\' if you would like to restart.')
    
        if choice == 'Y' or choice == 'y':
            print('Restarting now...')
            os.execl(sys.executable, sys.executable, *sys.argv)
    
        else:
            print('Thank you for using the tool!')
            print('The program will close in 10s...')
            time.sleep(10)
    

    So the user can input an option 'Y/N' to restart the program or not.

提交回复
热议问题