Batchography: Embedding Python scripts in your Batch file script

I keep writing about Batch programming, so it is obvious by now that Batch files programming has become one of my favorite activities. Every time I have to write a quick script to automate a task, I go first for the Batch files programming language. If that does not do the job, I use the Python programming language and if that fails, I go for C/C++ before deciding to writing using the assembly language.

Now, what about combining the two languages to achieve what you want?

That’s today’s topic. It is an excerpt from Chapter 4 in the Batchography book.

Everyone knows that Python is a much more powerful and capable language than the Batch files scripting language, so why would one want to combine the two?

One reason comes to mind: to use the convenience and ease of the Batch files scripting language to do the initial environment set up, then let Python execute. I have seen this combination often times in build systems, where the Batch part of the script setups up the compiler path, creates folders, checks for the existence of 3rd party tools and then finally lets Python execute.

To embed Python inside a Batch script, we have to make use of two Python features:

  1. Running the Python interpreter with the “-x” switch
  2. Using the multiline strings of the Python language

Let me show you an example script and then proceed to explaining it afterwards:

@echo off
rem = """
:: From here and on, write any Batch file syntax and it will be ignored by Python

:: 
:: The Batchography book by Elias Bachaalany
::

python -x "%~f0" %*
exit /b %errorlevel%
:: End of batch file commands
"""

# Anything here is interpreted by Python

import platform
import sys

print("Hello world from Python %s!\n" % platform.python_version())
print("The passed arguments are: %s" % sys.argv[1:])

When Python is executed with the “-x” switch, it will skip the first line, but anything after line 1 will be treated as a Python script and therefore it should be valid syntax. Line 1, which is interpreted as Batch script, turns off the command echo.

Line 2 is a valid syntax for both Python and Batch. In Batch, the REM keyword ignores the rest of the line and in Python, the syntax: “REM = ” simply defines a variable called “rem” and assigns to it the multiline string (starting from line 2 to line 11). From the Python’s interpreter’s perspective, lines 2 to 11 are part of the multiline string rem. From the Batch script perspective, those lines contain valid Batch script syntax. In between lines 3 to 11, you can write as much Batch script syntax as you want until you decide to invoke the Python interpreter (at line 8) and exit the Batch script using either the “GOTO :eof” or “EXIT /b” syntax (at line 9).

Let’s save the script as batch-python.bat and run it:

c:\Batchography>batch-python arg1 arg2 arg3
Hello world from Python 2.7.6!

The passed arguments are: ['arg1', 'arg2', 'arg3']

c:\Batchography>

It is as simple as that! You can learn more in details how to embed other interpreted languages such as Powershell or Perl from the Batchography book!

flower separator
batchography-good-resDo you want to master Batch Files programming? Look no further, the Batchography is the right book for you.

Available in print or e-book editions from Amazon.
flower separatorYou might also like:

Leave a Reply