如何编写shell脚本在循环中运行python?

时间:2014-08-05 16:28:23

标签: python shell

我有以下python代码:

t1=datetime.datetime.now()
t2=t1-datetime.timedelta(days=3)
sql="select * from tbl1 where thedate between %s and %s"%(t2,t1)

python代码是从t1到t2的选择数据,现在我想编写一个shell脚本来设置从2014.1.1到今天的t1。并运行每天的python代码。我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

我不明白为什么你需要一个shell脚本。如果你玩日期时,你可以在python中完成。

t=datetime.datetime.today()
today=datetime.date(today.year, today.month, today.day) # needs to be a date object
t1=datetime.date(year=2014, month=1,day=1) # set initial date
while t1 < today : # from initial date to today
    t2=t1-datetime.timedelta(days=3*365)
    sql="select * from tbl1 where thedate between %s and %s"%(t2,t1)
    t1+=datetime.timedelta(days=1) # increment day

希望这有帮助!