Restarting a self-updating python script

前端 未结 8 727
小鲜肉
小鲜肉 2020-11-30 18:48

I have written a script that will keep itself up to date by downloading the latest version from a website and overwriting the running script.

I am not sure what the

相关标签:
8条回答
  • 2020-11-30 19:18

    You can use reload(module) to reload a module.

    0 讨论(0)
  • 2020-11-30 19:20

    I think the best solution whould be something like this:

    Your normal program:

    ...
    
    # ... part that downloaded newest files and put it into the "newest" folder
    
    from subprocess import Popen
    
    Popen("/home/code/reloader.py", shell=True) # start reloader
    
    exit("exit for updating all files")
    

    The update script: (e.g.: home/code/reloader.py)

    from shutil import copy2, rmtree
    from sys import exit
    
    # maybie you could do this automatic:
    copy2("/home/code/newest/file1.py", "/home/code/") # copy file
    copy2("/home/code/newest/file2.py", "/home/code/")
    copy2("/home/code/newest/file3.py", "/home/code/")
    ...
    
    rmtree('/home/code/newest') # will delete the folder itself
    
    Popen("/home/code/program.py", shell=True) # go back to your program
    
    exit("exit to restart the true program")
    

    I hope this will help you.

    0 讨论(0)
提交回复
热议问题