How to Run a Batch File From Python
In this tutorial, you will run a Windows batch file using Python. We also have tutorial on how to create a batch file that runs a Python script.
TLDR solution
run_bat.py
import subprocess
subprocess.run("path-to-file/file_name.bat")
Step-by-Step Example
Let's say, you want to create a script that runs a batch file located on your desktop, which gets the version number of your system's Windows.
The batch file:
ver.bat
ver
The script might look like this:
get_ver.py
import os
import subprocess
path = os.path.expanduser("~/Desktop") + "/ver.bat"
subprocess.run(path)
After you run this code, the version number of your Windows will be printed to your shell.
That's it! You just learned how to run a Windows batch file using Python.