Schedule Your Python Scripts On Windows Platform | by Ujjwal Dalmia | Towards Data Science

Let’s think of a scenario before we start with this tutorial:
Your manager calls you and instructs, “the client wants refreshed data every day by 6 AM IST. Please change your shift timings and ensure script execution post client’s batch completes”. What does this mean? This means, starting today, you will start losing your precious sleep, time with family and friends, and above all, will be working on executing some random script at the most unproductive hour of the day.
If the above sounds familiar and you want to save yourself from a nightmare like this, please read further. In this tutorial, we will learn to schedule Python scripts on a Windows platform.
The achieve our objective, we need 3 things:
Python Script —For this tutorial, I have written a small Python code that reads a “CSV” file from my Windows folder location. This “CSV” file contains 2 columns, each having random numbers. The code adds both the columns to create a new one and saves this new version of the CSV file at the same folder location.
#### Importing the required libraryimport pandas as pd#### Reading csv filedf = pd.read_csv("C:\\Ujjwal\\New_File_V1.csv")#### Adding 2 columnsdf["Third_Column"] = df["Randome Numbers 1"] + df["Random Numbers 2"]#### Exporting the data to same locationdf.to_csv("C:\\Ujjwal\\New_File_V2.csv",index = False)
Batch File — A batch file is a script file with a “.BAT” extension. These files are generally saved as simple text files and contain commands which can be executed on the command-line interface (command prompt). When the command prompt executes these files, it reads through the commands written in the file and executes them line by line.
Scheduler — Last but not the least, we need a scheduler that can read the Batch file and executes the commands written in it at a set time. For this purpose, we will use Windows’ Task Scheduler will come in handy.
In the previous section, we have already shared the sample Python code which we plan to schedule. The second component required is a Batch file. For our purpose, the Batch file will have 2 commands:
Location of the Python Application— This is the Python application (“.exe” extension) which is used for the execution of the scripts. In our batch file, we will provide the location of this application as the first command. On my system, this location is as below:
#### Location of Python ExecutableC:\Users\Ujjwal\Anaconda3\python.exe
Location of the Python Script — This is the script that you want to schedule. We will provide the location of this script as the second command to the Batch file. Given we are working with Python scripts, ensure that the backslashes in the folder location are replaced with forward slash. The Windows location of my script is as follows.
#### Windows LocationC:\Ujjwal\Executable.py#### Backslash replaced with forward slashC:/Ujjwal/Executable.py
Final Script — The final Batch script will look something like below. Save this script in a text file with a .BAT extension at any Windows location.
#### Final Batch ScriptC:\Users\Ujjwal\Anaconda3\python.exe "C:/Ujjwal/Executable.py"
In this step, we will set up the schedule to execute our tasks periodically. The step by step process is as follows:
Open Windows Task Scheduler — Go to Windows search, search for task scheduler, and open it. The task scheduler interface will look something like below:
Create the New Task — Click on “Create Basic Task” in the right window pane to create a new task. A new window, prompting you to fill the task name and description will pop-up. The interface will look like below:
Select Periodicity — Click next after filling the name and description details of the task. Now, the system will prompt you to select the trigger options where we can set the periodicity of the task. For our purpose, we are selecting the daily execution. A complete list of trigger options is as follows:
Select Schedule Time — Click next after selecting the task frequency. Now, the system will prompt you to select a specific time at which you want the schedule to trigger. The interface screen would look something like below:
Create Action — Click next after selecting the execution time. In this step, the scheduler will prompt you to select the action you want to schedule. Select “Start a program”. The interface will look like below:
Select Batch File — Click next after selecting the desired action. Now, the application interface will ask for the location of the program you want to schedule. Select the batch file which we had created in the previous section. The application screen will look as follows:
Finally — That’s it, click next and finish. Your Python script will execute at a specific time every day.
I am sure that with a solution like above, you can automate all your repetitive tasks.
I hope this tutorial was informative and you learned something new. Please comment and share your feedback.
Stay tuned for more interesting topics for next time. Till then:
HAPPY LEARNING ! ! ! !