简体   繁体   English

如何使用python在可执行输出中搜索文本字符串?

[英]How to search for text string in executable output with python?

I'm trying to create a python script to auto update a program for me.我正在尝试创建一个 python 脚本来为我自动更新程序。 When I run program.exe --help, it gives a long output and inside the output is a string with value of "Version: XXX" How can I make a script that runs the command and isolates the version number from the executable's output?当我运行 program.exe --help 时,它会给出一个很长的输出,并且在输出中是一个值为"Version: XXX"的字符串如何制作一个脚本来运行命令并将版本号与可执行文件的输出隔离开来?

I should have mentioned that I tried the following:我应该提到我尝试了以下方法:

import re 
import subprocess

regex = r'Version: ([\d\.]+)'

match = re.search(regex, subprocess.run(["program.exe", "--help"])) 

print((match.group(0)))  

and got the error:并得到错误:

Traceback (most recent call last):
  File "run.py", line 6, in <module>
    match = re.search(regex, subprocess.run(["program.exe", "--help"]))
  File "C:\Python37\lib\re.py", line 183, in search
    return _compile(pattern, flags).search(string)
TypeError: expected string or bytes-like object

这样的事情应该工作:

re.search(r'Version: ([\d\.]+)', subprocess.check_output(['program.exe', '--help']).decode()).group(1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM