“schedule” module to automate your Python script.

Rahul Keshav
2 min readDec 27, 2022

--

Photo by CHUTTERSNAP on Unsplash

In order to put things into perspective, I had to create a Python script that would extract data from a website, model it, and convert it to a CSV file. The script must be performed once daily because the data was updated daily. If you’re interested, you can view the entire script here. However, automation — rather than the script itself — is the focus of this blog.

After several fruitless attempts to automate the process, I discovered the schedule Python module. There is a straightforward, one-liner answer to every automation issue!

Here is a little illustration (taken from the official page of the modules):

import schedule
import time

def job():
print("I'm working...")

schedule.every(10).seconds.do(job)
schedule.every(10).minutes.do(job)

It pretty much goes without saying. by include schedule.every(10).seconds in the line. do(job), which will set the job() function to execute once every ten seconds.

To return to my project and solution, I put the schedule module into practise as follows:

#Run the Script
#run_script being the function I want to run
schedule.every().day.at('05:00').do(run_script)
while True:
schedule.run_pending()
time.sleep(1)

To finish the task, I had to construct an infinite While loop that would execute once every second. Sch.every().day.at(‘05:00’) is the line in question. The command do(run script) failed to execute my script. This line initialises the extraction time and frequency, but it must be “called” by: sch.run pending ().

And voila! Every day at 5 AM, my script will run and the data extraction will be done.

Pros :

Quite a basic automation task solution
Practical if the script is implemented on a server (which is what I’m doing for this project), as it will execute continuously there.
Con :

If the script is not running when you run it locally, the automation will not function.
you can use cron or window task manager to do the same if you planning to close the script after execution.

similarly, with the use of schedule and plyer, you can create a notification thing for yourself.

Thank you for reading!

--

--

Rahul Keshav

Hi, I am a data science enthusiast with a passion for automating processes and uncovering insights from data.