views:

376

answers:

3

Hi Everyone,

I am really new to Python, and programming in general, but I have a python script I would like to run at regular intervals. I am running windows 7. What is the best way to accomplish this? Easiest way?

Any help you can provide will be very much appreciated!

Brock

A: 

The first thing to try is the windows task scheduler itself. Press the "start" button and type "task scheduler" to open it.

redtuna
+1  A: 

A simple way to do this is to have a continuously running script with a delay loop. For example:

def doit():
    print "doing useful things here"

if __name__ == "__main__":
    while True:
        doit()
        time.sleep(3600) # 3600 seconds = 1 hour

Then leave this script running, and it will do its job once per hour.

Note that this is just one approach to the problem; using an OS-provided service like the Task Scheduler is another way that avoids having to leave your script running all the time.

Greg Hewgill
+3  A: 

You can do it in the command line as follows:

schtasks /Create /SC HOURLY /TN PythonTask /TR "PATH_TO_PYTHON_EXE PATH_TO_PYTHON_SCRIPT"

That will create an hourly task called 'PythonTask'. You can replace HOURLY with DAILY, WEEKLY etc. PATH_TO_PYTHON_EXE will be something like: C:\python25\python.exe. Check out more examples by writing this in the command line:

schtasks /?

Otherwise you can open the Task Scheduler and do it through the GUI. Hope this helps.

Glen Robertson