How to Run a Batch File from Python

In this short tutorial, I’ll show you the steps to run any batch file from Python. I’ll use a simple example to demonstrate this concept.

Steps to Run a Batch File from Python

Step 1: Create the batch file

To start, create your batch file.

For demonstration purposes, I created a simple batch file that would produce The Matrix effect, but the method described here would work for any batch file that you’d like to run from Python.

You may then open Notepad and copy the code below:

@echo off
color 0a
mode 1000
 
:a
 
echo %random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%%random%
 
ping > nul
ping > nul
ping > nul
 
goto a

To create the batch file, save the Notepad as Matrix.bat

Don’t forget to put the file extension of .bat when saving your file:

Batch File

Step 2: Write the Python code

Here is the code structure that you can use to run a batch file from Python:

import subprocess
subprocess.call([r'path where the batch file is stored\name of the batch file.bat'])

Note: The above code structure should work in Python, but it may hang in Anaconda.

For our example:

  • The path where I stored the batch file is: C:\Users\Ron\Desktop\Run Batch
  • The name of the batch file is: Matrix.bat

So this is the code that I used to run the batch from Python:

import subprocess
subprocess.call([r'C:\Users\Ron\Desktop\Run Batch\Matrix.bat'])

You’ll need to modify the Python code to reflect the location where the batch file is stored on your computer.

Step 3: Run the Python code

Finally, run the Python code, and you’ll get The Matrix effect:

How to Create The Matrix Effect using a Batch File

In the next section, I’ll share the code to create a simple Graphical User Interface (GUI) to run the batch file from the GUI itself.

Create a GUI that can start the batch file

How about creating a simple Python GUI that can run the batch using a button click?

You can certainly do that using the code below. As before, you’ll need to modify the Python code to reflect the path where the batch file is stored on your computer.

import subprocess
import tkinter as tk

root= tk.Tk() 
   
canvas1 = tk.Canvas(root, width = 350, height = 250) 
canvas1.pack()

def start_batch(): 
       subprocess.call([r'C:\Users\Ron\Desktop\Run Batch\Matrix.bat'])
           
button1 = tk.Button (root, text='Run The Matrix ',command=start_batch)
canvas1.create_window(170, 130, window=button1)

root.mainloop()

Run the Python code, and you’ll see this GUI:

How to Run a Batch File from Python

Once you click on the button, you’ll get The Matrix effect:

How to Create The Matrix Effect using a Batch File

Now what if you want the other way around?

More specifically, what if you want to run a Python script using a batch file?

To do that, you can check this tutorial that explains how to create a batch file to run a Python script!

Finally, you may want to check the subprocess documentation for additional information about this module.