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.
We click the final prompt and press ‘Finish’. Our batch script should now run daily at 1 pm!
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:
Thanks for reading!