【问题标题】:Store the output of Subprocess.call in a String将 Subprocess.call 的输出存储在字符串中
【发布时间】:2015-01-26 23:40:24
【问题描述】:

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

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

#这返回我 0

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

【问题讨论】:

    标签: python unix subprocess


    【解决方案1】:
    x=subprocess.Popen("grep 'xyz-pqr' textfile_5906.txt",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
    output,err=x.communicate()
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-07
      • 2011-01-30
      • 1970-01-01
      • 2011-08-19
      • 1970-01-01
      • 2013-03-22
      • 1970-01-01
      • 2018-08-11
      相关资源
      最近更新 更多