A batch file is a script that contains a series of commands saved as plain text and can be executed on MS-DOS. It can be saved with the .bat or .cmd extension. This article will discuss how batch files can be executed using Python.
Create a batch file
On Windows, create a plain text file using a normal editor like Notepad and then rename the text file with the .bat or .cmd extension. This is now a batch file.
Add commands into the batch file
For demonstration purposes, we will create a batch file named example1.bat with the following simple commands.
1 2 3 4 |
@echo off echo Hello_World echo %TIME% %DATE% REM This is a comment |
Executing the batch file with these commands prints out “Hello World” and system time on the first line of the output console, and date on the second line.
When a line is preceded by the @ symbol like in line 1, the running command, echo, in this case, is hidden, and echo off turns off echoing all commands running on the script.
REM is a comment command on bash.
Write Python code to run the batch file
Two packages, namely, os and subprocess, can be used to execute batch files via Python. The general syntax for each of the packages is shown below.
os:
1 2 |
import os os.system("path/to/the/location/of/batch_file") |
subprocess:
1 2 |
import subprocess subprocess.call(["path/to/the/location/of/batch_file"]) |
Assume that the batch file is saved as on disk C (“C:\ example.bat”) then the code for running the batch file from Python using subprocess is:
1 2 |
import subprocess subprocess.call([r"C:\example.bat"]) |
“r” appearing before the path string is a vital string format to ensure that the path is treated as a row string. Specifically, this string formatting ensures that backslash is treated as a literal character and not an escape character. If we do not use “r” formatting, “\b” is treated as backspace in our example.
Alternatively, the backslash (\) on the Windows path is replaced with a forward slash (/).
Execute the Python script
Once the code has been written, it can be saved as a Python .py file and executed to run the commands on the batch file. The following output is obtained when the Python script is executed.
Passing Arguments into a Batch File in Python
You can also run a batch file in Python with arguments passed on the Python script. The general syntax for passing arguments using the two packages, os, and subprocess, is shown below:
os:
1 2 |
import os os. system("path/to/the/batch_file arg1 arg2 …") |
Note that the arguments come inside the command string with white spaces between arguments.
subprocess:
1 2 |
import subprocess subprocess.call(["path/to/the/batch_file", arg1, arg2, …]) |
In the subprocess module, the arguments come after the path and are separated by a comma. The arguments must also be strings.
Let’s modify the batch file and the code we wrote earlier. The batch file now contains two arguments, arg1 and arg3.
1 2 3 4 |
@echo off echo Passing arguments set arg1=%1 & set arg3=%3 echo arg1 = %arg1%, arg2 = %arg3% |
The argument arg1 is set to the value of the argument in position one and arg3 to the value in the third position on the Python code.
import os
1 2 |
import os os.system("example.bat 2 9 13") |
In this code, values for arguments in position 1, 2, and 3 is 2, 9, and 13, respectively. Executing the Python scripts containing this code gives the following output.