How to Create a Batch File Directly from Python

In this short guide, I’ll show you 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:

myBat = open(r'Path to store the new batch file\File name.bat','w+')

myBat.write('command to be included in the batch file')

myBat.close()

Let’s now see the steps to apply the above template in practice.

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.

For example, in my case, I chose to store the batch file under the following path:

C:\Users\Ron\Desktop\Test

Path in Windows

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, I specified a basic command that will display the current date in green:

cmd /k "color a & date

Step 3: Create the batch file directly from Python

Finally, use the following template to help your create the batch file directly from Python:

myBat = open(r'Path to store the new batch file\File name.bat','w+')

myBat.write('command to be included in the batch file')

myBat.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: mytest1
  • The command to be included in the batch file is: cmd /k “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):

myBat = open(r'C:\Users\Ron\Desktop\Test\mytest1.bat','w+')

myBat.write('cmd /k "color a & date"')

myBat.close()

Run the code in Python, and a new batch file will be created at your specified location:

Folder in Windows

Double-click on the batch file, and you’ll see the current date in green:

How to Create a Batch File Directly from Python

Command with Multiple Lines

What if you have a command with multiple lines? How would you then create the batch file from Python?

If that’s the case, you’ll need to place triple quotes around the batch command.

Here is an example of a batch command that will create the Matrix effect. This command has multiple lines.

So this is the full code to create a batch file that contains multiple lines:

myBat = open(r'C:\Users\Ron\Desktop\Test\mytest2.bat','w+')

myBat.write('''@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''')

myBat.close() 

Run the code in Python (adjusted to your path), and you’ll see the new file (‘mytest2’):

Create a Batch File Directly from Python

Once you double-click on the new batch file, you’ll see the matrix effect:

How to Create a Batch File Directly from Python

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\mytest3.bat'])

So your complete Python code would look like this:

import subprocess

myBat = open(r'C:\Users\Ron\Desktop\Test\mytest3.bat','w+')

myBat.write('''@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''')

myBat.close() 


subprocess.call([r'C:\Users\Ron\Desktop\Test\mytest3.bat'])

Once you run the code in Python, the matrix effect will appear on your screen:

Example of Creating a Batch File Directly from Python

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