gpt4 book ai didi

python - 将 Subprocess.call 的输出存储在一个字符串中

转载 作者:太空宇宙 更新时间:2023-11-04 01:11:58 24 4
gpt4 key购买 nike

我正在尝试通过 python 代码执行 unix 命令。但是当我使用 subprocess.call 函数时,它会直接弹出结果。我想将该结果存储到一个字符串中。下面是我的代码示例。

import subprocess
Store_result=subprocess.call("grep 'xyz-pqr' textfile_5906.txt",shell=True)
print Store_result

#这返回我 0

我想将结果存储在一个字符串中,并将其用于进一步编码。

最佳答案

x=subprocess.Popen("grep 'xyz-pqr' textfile_5906.txt",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
output,err=x.communicate()

这样你就可以有错误和输出。

关于python - 将 Subprocess.call 的输出存储在一个字符串中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27186513/

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