In this short guide, you’ll see how to create a batch file directly from Python.
To begin, here is a template that you can use to create your batch file from Python:
my_bat = open(r'Path to store the new batch file\File name.bat', 'w+') my_bat.write('command to be included in the batch file') my_bat.close()
Steps to Create a Batch File Directly from Python
Step 1: Capture the path to store the new batch file
To start, capture the path to store your new batch file.
Here is an example of a path where the new batch file will be stored:
Step 2: Specify the command to be included in the batch file
Next, specify the command to be included in the batch file.
For simplicity, let’s specify a basic command that will display the current date in green:
color a & date
Step 3: Create the batch file directly from Python
Finally, use the following template to create the batch file directly from Python:
my_bat = open(r'Path to store the new batch file\File name.bat', 'w+') my_bat.write('command to be included in the batch file') my_bat.close()
For our example:
- The path to store the new batch file is: C:\Users\Ron\Desktop\Test
- The new file name to be created is: my_test_1
- The command to be included in the batch file is: color a & date
So the complete Python code would look as follows (you’ll need to modify the path to reflect the location where the new batch file will be created on your computer):
my_bat = open(r'C:\Users\Ron\Desktop\Test\my_test_1.bat', 'w+') my_bat.write('color a & date') my_bat.close()
Run the code in Python, and a new batch file will be created at your specified location.
Double-click on the batch file, and you’ll see the current date in green:
'C:\Users\Ron\Desktop\Test>color a & date' 'The current date is: Sat 03/23/2024' 'Enter the new date: (mm-dd-yy)'
Command with Multiple Lines
What if you have a command with multiple lines?
In that case, you’ll need to place triple quotes around the batch command.
Here is an example of a Python code to create a batch file that contains multiple lines:
my_bat = open(r'C:\Users\Ron\Desktop\Test\my_test_2.bat', 'w+') my_bat.write('''@echo off color a & date pause ''') my_bat.close()
Run the code in Python (adjusted to your path), and you’ll get the new file (‘my_test_2’).
Once you double-click on the new batch file, you’ll see the current date in green as follows:
'The current date is: Sat 03/23/2024' 'Enter the new date: (mm-dd-yy)'
Run the Batch File from Python
Once you created the batch file, you can also run it directly from Python.
Simply add this line at the top:
import subprocess
Then, add this line at the bottom (adjusted to the path where the batch file is stored on your computer):
subprocess.call([r'C:\Users\Ron\Desktop\Test\my_test_3.bat'])
So the complete Python code would look like this:
import subprocess my_bat = open(r'C:\Users\Ron\Desktop\Test\my_test_3.bat', 'w+') my_bat.write('''@echo off color a & date pause ''') my_bat.close() subprocess.call([r'C:\Users\Ron\Desktop\Test\my_test_3.bat'])
Once you run the code in Python, the current date will appear on your screen without clicking on the batch file:
'The current date is: Sat 03/23/2024' 'Enter the new date: (mm-dd-yy)'