if语句的逻辑,用于在Python中基于异常执行代码

时间:2015-03-27 01:06:15

标签: python

我想在Windows任务计划程序中每五分钟运行一个脚本。该脚本读取服务请求的JSON Web服务,并将指定的字段写入esri地理数据库。

对于此过程,有一个时间戳,其中包含查询每个服务请求的时间的信息。

我的问题是我如何在这个剧本中创建逻辑来说"嘿,我在12:00-12:05:59 PM成功运行,12:06-12:11发生了一些事情:59,但我会跑12:11-12:16:59,因为我很好,我会抓住12:06-12:11的数据。"

到目前为止我在代码中构建的逻辑是;

import datetime


DateofDataCreation = 2015-02-17 16:53:25
i = 5
Start = datetime.datetime.now()
now_minus_5 = Start - datetime.timedelta(minutes =i)


if DateofDataCreation >= now_minus_5:
    WriteToDatabase
else:
    print "No Current Data"

1 个答案:

答案 0 :(得分:1)

我不确定我是否完全理解你的问题。根据我的想法,试试这个:

import datetime

#If you change the time to be after now() then it will print "WriteToDatabase" .
#If the time is before then it will print "No Current "Data"
DateofDataCreation = datetime.datetime(2015,2,17,16,53,25)

i = 5

Start = datetime.datetime.now()
now_minus_5 = Start - datetime.timedelta(minutes =i)



if DateofDataCreation >= now_minus_5:

     print("WriteToDatabase")
else:
     print ("No Current Data")