从bat文件中读取文本到python [英] Reading text from a bat file in to python

查看:265
本文介绍了从bat文件中读取文本到python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从批处理文件中读取一行到python脚本中?

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

我有一个.bat文件,其中包含以下内容:

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
...

我如何从该.bat文件中获取A_version并将其存储为变量. 我将以某种方式需要读取bat文件并查找A_version并将其9.0.0的值存储到python中的变量中.

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.

任何帮助将不胜感激.

谢谢.

推荐答案

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.

这篇关于从bat文件中读取文本到python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆