Coder Coder - 1 year ago 141
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.


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

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download