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

A Two-Part Post that Shows You How to Automate Your Python Script Execution

PART 1: Create a Batch File to Execute a Python Script

One of the most important things I do on my virtual machine is automate batch jobs. Let’s say I want to send Susie an automated email reported generated via a Python script at 6 am every day—I want to automate the task of generating and sending that email via Python using a batch job on my virtual machine (VM). This tutorial walks you through automating the process and setting up your computer (or VM) to run Python jobs on a schedule.

First, we select a Python script that we want to automatically execute. The one I’ve picked below is named python_example_script.py:

"""
This is the sample Python file that we want to automate in batch mode
"""
import pandas as pd

def main():
    #Create a dummy pandas dataframe 
    dummy_df=pd.DataFrame({'col1': [1, 2], 'col2': [3, 4]})
    #Write the dataframe to a csv
    dummy_df.to_csv('sample_dummy_file.csv')
    print('Automating the boring work makes our lives so much better!')
    
if __name__== "__main__":
  main()

Above is a very simple Python script, where we generate a pandas dataframe and save it to a csv, called sample_dummy_file.csv. We then output a print statement.

Next, we create a .bat file that will automatically execute the Python script from the Windows command line.

We want to call the Python script directly from the Command Prompt and execute it. I have saved the Python script that I want to execute under my Documents/Blog/BatchMode directory, so I locate the directory path:

Locate the Python script in the File Folder that we want to automate

Highlighted is the Python file that we want to automate, python_example_script.py, as well as the Directory Path.

I open a fresh Notepad document and type the following:

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

python python_example_script.py

Let’s walk through what the above script means:

The ‘cd’ command at the beginning references the change directory. It is the OS command to change the current working directory.

The directory that we want to point to in question is referenced immediately after cd—”C:\Users\Documents\Blog\BatchMode”. We need to point to this directory for the command prompt to find the python_example_script.py file.

To get the EXACT directory path that we want to reference, we right click on the ‘BatchMode’ file path in the file folder, and press ‘Copy address as text’. This will save the exact directory path that we want to use, and we can paste it right after the cd command.

Get the file directory name that your Python file is sitting in

On the second line of the file is the ‘python’ statement. This is telling the Command Prompt that we want the Python.exe application to perform an action; in this case, execute the python_example_script.py, which is right after the ‘python’ command.

IMPORTANT: For the Command Prompt to recognize ‘python’ as an application, the python application needs to be added to the Windows PATH. When you first installed Python, you had the option to add Python to the Windows Path. If you enabled this option—great, you should be good to go! If not, you’ll need to add Python to the Windows Path. This tutorial walks you through how to do this.

Finally, we want to save the batch file that we have just created in Python. When we save the file, we want to save it as a .bat file, as shown below:

Be sure to save your file as a .bat extension!

Be sure to use ‘All Files’ under the ‘Save as type’ option, or you won’t be able to save the file as a .bat extension!

Finally, we execute the batch file to ensure that it works properly.

We find the .bat file in the in the BatchMode directory that we just created, and we simply press on it. The Command Prompt should automatically open, and the script should start executing, as shown below:

Executing the Python batch file: the python file will execute via the command line when the batch file is manually pressed

As you can see, the Command Prompt opens in the “C:\Users\Documents\Blog\BatchMode” directory, and we execute the python_example_script.py file!

This concludes Part 1 of this tutorial. If you want to learn how to schedule this Python file to run automatically using Windows Task Scheduler, read on to Part 2 of this tutorial.

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!

4 comments

Leave a Reply

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