Is this doable

MRAB google at mrabarnett.plus.com
Fri Mar 21 14:11:07 EDT 2008


On Mar 21, 11:48 am, fkallgren <fkallg... at gmail.com> wrote:
> Hi.
>
> I have a little problem. I have a script that is in the scheduler
> (win32). But every now and then I update this script and I dont want
> to go to every computer and update it. So now I want the program to 1)
> check for new version of the script, 2) if there is a new version,
> copy that verision from server to local drive, 3) shutdown the program
> and start it up again as the new version.
>
> The problem is that I can't run this script directly from server so it
> have to run it locally.
>
> Anyone having any bright ideas??
>
The script could just check to see if the version on the server is
more recent and if it is then copy it over the local one, start the
local one, and then quit.

Python compiles the script to bytecode and then interprets the
bytecode, so when the script is being run the .py or .pyw source
itself isn't being used and can be overwritten. I've tried the
following on Windows XP and it works:

import os
import sys
import shutil

# Is there a newer version?
my_path = sys.argv[0]
update_path = os.path.join(os.path.dirname(my_path), "new_script.py")

if os.path.getmtime(my_path) < os.path.getmtime(update_path):
    # Update the script.
    shutil.copy2(update_path, my_path)
    # Re-start the script.
    os.startfile(my_path)
    sys.exit()

# The rest of the script...



More information about the Python-list mailing list