How to Run a Batch File from Python

Often times you may want to run a batch file from Python itself.

And so, in this short tutorial, I’ll show the steps to run any batch file from Python!

I will also demonstrate how to create a simple Python GUI to run the batch file from the GUI itself.

To start, I’ll review an example of running a simple Batch file that creates the ‘Matrix Effect.’

I used this type of a batch file for illustration purposes (and also because I like the Matrix), but the methodology described here would work for any batch file that you’d like to run from Python.

Just saying…

The Batch File

Let’s start by creating the batch file. As mentioned before, I will use the following code to create the Matrix effect, but you can use any other code that works for you.

(1) First, open Notepad and copy this code:

 

@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 1.1.1.1 -hh -w 0.2>NUL
 
goto a

 

(2) 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

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 that the above code structure should work on Python, but it may hang if you’re using Anaconda.

Applying that code structure to our example:

 

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

 

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

  • First, type the path where your batch file is stored (instead of using my path of: ‘C:\Users\Doron E\Desktop\Run Batch\’)
  • Then, type the name of your Batch file (instead of using ‘Matrix.bat’ – unless you saved the batch file with that name). Do not forget to put the extension of ‘.bat’ at the end of the batch file name

Create a GUI that can start the batch file

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

You can certainly do that.

Here I created a simple GUI with only 1 button.

And that button will run the batch file when I click on it.

I called that button ‘Run the Matrix’ and placed it at the center of the GUI.

As before, you’ll need to modify the Python code to reflect the path where your batch file is stored.

Here is the full Python code:

 

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\Doron E\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 a Batch File from Python

Press F5 to run the Python code. This is what you’ll see:

 

How to Run a Batch File from Python

 

Are you ready?

If so, click on the button ‘Run The Matrix’ and you’ll see the Matrix Effect

Note that I’m not responsible if you get sucked into those green digits…

 

How to Create The Matrix Effect using a Batch File

Conclusion

We have seen how to run a batch file from Python.

But what if you want the other way around?

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.