Python to run a piece of code at a defined time every day

人走茶凉 提交于 2019-12-22 19:24:12

问题


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.

How may I do it please?


回答1:


You can use "schedule" library

to install, on terminal enter:

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

good luck!



来源:https://stackoverflow.com/questions/43670224/python-to-run-a-piece-of-code-at-a-defined-time-every-day

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!