如何在一天中的特定时间打印某些内容 [英] How to print something at a specific time of the day
问题描述
是否有可能让python 2.7在一天中的特定时间打印。例如,如果我在15:06运行程序并将其编码为在15:07打印立即执行任务,则会打印出来。因此,无论你在15:07点击程序时运行程序,它都会打印出立即执行任务。此外,此时可以每周打印一次吗?
Is it possible to have python 2.7 print something at a specific time of the day. For example if I ran the program at 15:06 and coded it to print "Do task now" at 15:07 it prints it. So no matter what time you ran the program once it hit 15:07 it would print "Do task now." In addition is it possible to have it print every week at this time?
推荐答案
我建议安装图书馆时间表,如果你'能够。
I would suggest installing the library schedule, if you're able to.
使用 pip安装计划
如果使用时间表,您的代码将如下所示:
Your code would look like this if utilizing schedule:
import schedule
import time
def task():
print("Do task now")
schedule.every().day.at("15:07").do(task)
while True:
schedule.run_pending()
time.sleep(1)
如果1s间隔太长,你可以根据需要调整 time.sleep(1)
来睡眠更长时间。这是日程安排库页面。
You can adjust time.sleep(1)
as necessary to sleep for longer if a 1s interval is too long. Here is the schedule library page.
这篇关于如何在一天中的特定时间打印某些内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!