python time

Dave Angel davea at ieee.org
Sun Mar 20 22:09:14 EDT 2011


On 01/-10/-28163 02:59 PM, ecu_jon wrote:
> I'm working on a script that will run all the time. at time specified
> in a config file, will kick-off a backup.
> problem is, its not actually starting the job. the double while loop
> runs, the first comparing date works. the second for hour/min does
> not.
>
> #python file
> import os,string,,time,getpass,ConfigParser
> from datetime import *
> from os.path import join, getsize
> from time import strftime,localtime
>
> config = ConfigParser.ConfigParser()
> config.read("config.ini")
> source = config.get("myvars", "source")
> destination = config.get("myvars", "destination")
> date = config.get("myvars", "date")
> time = config.get("myvars", "time")
> str_time=strftime("%H:%M",localtime())
>
> while datetime.now().weekday() == int(date):
>      while str_time == time:
>          print "do it"
>
>

You're comparing two objects in that inner while-loop, but since they 
never change, they'll never match unless they happen to start out as 
matched.

you need to re-evaluate str_time each time through the loop.  Make a 
copy of that statement and put it inside the loop.

You probably don't want those loops to be comparing to ==, though, since 
if you start this script on some other day, it'll never loop at all. 
Also, it'd be good to do some form of sleep() function when you're 
waiting, so you don't bog the system down with a busy-loop.

DaveA



More information about the Python-list mailing list