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:

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.

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 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:

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!
[…] https://techrando.com/2019/06/22/how-to-execute-python-scripts-automatically-on-your-windows-compute… […]
[…] Path during your Python installation and you want to reference Python from the Command Line (see this tutorial as an example), you’ll need to manually add your Python installation to the Windows Path. This […]
[…] 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, […]
[…] Financial Time Series Data into Python: Some Free Options How to Execute Python Scripts in Batch Mode using Windows Task Scheduler How to Execute a Task Hourly in Task […]