Coder Coder - 2 days ago 5
Python Question

Reading text from a bat file in to python

Is it possible to read a line from a batch file in to a python script?

I have a .bat file that contains the following:

@echo off
rem generated from template

Set A_version= 9.0.0
set App_name = test
...


How can i grab the A_version from this .bat file and store it as a variable.
I would somehow need to read the bat file and look for A_version and store the value of it which is 9.0.0 in to a variable in python.

Any help would be appreciated.

Thanks.

Answer
for line in open('name.bat', 'r'):
    if 'A_version' in line:
        A_version = line.split('=')[-1].strip()
        break

This will search for the string, assign the value to a variable of the same name and break out of the loop in case the variable is referenced later in the bat, we don't need that.

Comments