gpt4 book ai didi

python - 在 Python 中解析标准输出

转载 作者:太空狗 更新时间:2023-10-29 17:36:01 24 4
gpt4 key购买 nike

在 Python 中,我需要获取我需要在我的脚本中调用的外部二进制文件的版本。

假设我想使用 Wget在 Python 中,我想知道它的版本。

我会打电话

os.system( "wget --version | grep Wget" ) 

然后我将解析输出的字符串。

如何在 Python 的字符串中重定向 os.command 的标准输出?

最佳答案

一种“旧”方式是:

fin,fout=os.popen4("wget --version | grep Wget")
print fout.read()

另一种现代方式是使用 subprocess 模块:

import subprocess
cmd = subprocess.Popen('wget --version', shell=True, stdout=subprocess.PIPE)
for line in cmd.stdout:
if "Wget" in line:
print line

关于python - 在 Python 中解析标准输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2101426/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com