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
Solution 1:
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.
Solution 2:
Try something like this:
with open('file.bat', 'r') as file:
for line in file:
if 'A_version' in line:
value = line.split('=')[1].strip()
Solution 3:
if you want to do it from batch:
for /f "tokens=2 delims==" %%a in ('type "yourbatfile.bat"^|findstr /b /i "Set A_version="') do set var=%%a
find the correct line and split it by =. If needed, you can strip the space with
set var=%var: =%
Post a Comment for "Reading Text From A Bat File In To Python"