How To Make a Python Program Automatically Restart Itself

后端 未结 5 1770
一生所求
一生所求 2020-12-08 11:51

How do you make a python program automatically restart itself? So let\'s say there is a really simple program like:

    var = input(\"Hi! I like cheese! Do y         


        
5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-08 12:23

    I use terminal on my Mac to re-start some of my python scripts with the function below.

    import subprocess  
    def run_again(cmd):
        subprocess.call(["bash", "-c", "source ~/.profile; " + cmd])  
    

    Note: Don't forget the space character after "profile;" or the function may fail silently!

    Then at the bottom of my script to be re-started:

    if some_condition:  
        run_again("python my_script.py %s" % my_new_arguments)  
    

    For the original question about the cheese script:

    if var != 'yes':  
        run_again("python my_cheese_script.py")  
    

提交回复
热议问题