Python每天在定义的时间运行一段代码 [英] Python to run a piece of code at a defined time every day
本文介绍了Python每天在定义的时间运行一段代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的 python 程序中,我希望它在每个工作日的预定义时间运行一段代码,比如周一至周五下午 2 点.
In my python program, I would like it to run a piece of code at a pre-defined time every weekday, let say 2pm Mon - Fri.
请问我该怎么做?
推荐答案
可以使用schedule"库
You can use "schedule" library
安装,在终端输入:
pip install schedule
这是您想要的代码示例:
here is an example of the code you want:
#!/usr/bin/python
import schedule
import time
def job():
print("I am doing this job!")
schedule.every().monday.at("14:00").do(job)
schedule.every().tuesday.at("14:00").do(job)
schedule.every().wednesday.at("14:00").do(job)
schedule.every().thursday.at("14:00").do(job)
schedule.every().friday.at("14:00").do(job)
while True:
schedule.run_pending()
time.sleep(1)
或者您可以阅读文档查看其他功能点击此处
or you can read the documents to see the other functions Click Here
祝你好运!
这篇关于Python每天在定义的时间运行一段代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文