python 怎么定时每天在凌晨2点 输出hello word 也就是到时间执行print he

python 怎么定时每天在凌晨2点 输出hello word 也就是到时间执行print hello word 求具体代码 谢谢

定时执行一般需要符合以下条件

使用cron之类的计划任务程序来调用

程序在后台循环执行


后台循环一般代码:

import time

while True:
    current_time = time.localtime(time.time())
    if((current_time.tm_hour == 2) and (current_time.tmin == 0) and (current_time.tsec == 0)):
        print "Hello World"
    time.sleep(1)

追问

怎么我把current_time.tm_hour == 2 改成9点 就报错了 提示
AttributeError: 'time.struct_time' object has no attribute 'tmin'

追答

不好意思写错了:

if((current_time.tm_hour == 2) and (current_time.tm_min == 0) and (current_time.tm_sec == 0)):

温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答