Sometimes, we want to make a Python script to do something at the same time every day.
In this article, we’ll look at how to make a Python script to do something at the same time every day.
How to make a Python script to do something at the same time every day?
To make a Python script to do something at the same time every day, we can use the schedule module.
To install it, we run
pip install schedule
Then we use it by writing
import schedule
import time
def job(t):
print('working', t)
return
schedule.every().day.at("01:00").do(job, 'It is 01:00')
while True:
schedule.run_pending()
time.sleep(60)
to create an infinite while loop that calls schedule.run_pending to run the job function every day at 01:00.
We specify the schedule of the job with
schedule.every().day.at("01:00").do(job, 'It is 01:00')
We call every and use the day property to run the job every day.
And we call at with '01:00' to run the job at 01:00.
Conclusion
To make a Python script to do something at the same time every day, we can use the schedule module.