How to Execute Python Scripts in Batch Mode using Windows Task Scheduler

PART 2: Schedule a Batch File to Run Automatically Using Task Scheduler

Welcome to Part 2 of my series on automating Python scripts to run on a schedule! In Part 1, we go over generating a batch file to run a Python script from the command line. In Part 2, we’ll go over how to automate the execution of a script using its associated batch file in Task Scheduler.

First, we create a .bat file that properly executes our Python code.

We will be using the .bat file that we generated in Part 1 for the python_example_script.py file, which contains the following code that allows the Python script to execute directly from the Command Prompt:

cd "C:\Users\Documents\Blog\BatchMode"

python python_example_script.py

Next, we set up the batch file to execute on a timer using Windows Task Scheduler.

We open Task Scheduler, go to the Action Tab, and select ‘Create Basic Task’:

A Basic Task popup appears, and we name and describe the process:

We select the Next button, and then set the frequency at which we want to start the script (here we choose daily, meaning the batch file will execute once a day):

Next, we select the Next button, and then set the execution time of the batch file at 1 pm daily, starting with the current day:

We select ‘Start a Program’ in the next tab:

Finally, we are prompted to select the Program/Script that we want to execute on a schedule. We press the ‘Browse’ button, and then go to the directory containing the batch file that we want to execute. We select the execute_python_file.bat file and open it.

Select the .bat file that we want to execute on a timer

We click the final prompt and press ‘Finish’. Our batch script should now run daily at 1 pm!

The task is ready to be executed!

This concludes Part 2 of this tutorial. Part 1 covered how to create the .bat file to run a Python script from the command line.

As always, the code for this tutorial is available for your viewing pleasure via my GitHub account, under the following URL:

https://github.com/kperry2215/batch_mode_script_automation

Thanks for reading!

2 comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.