简体   繁体   English

将Subprocess.call的输出存储在字符串中

[英]Store the output of Subprocess.call in a String

I am trying to hit a unix command through python code.But when I am using the subprocess.call function it is directly poping the result. 我试图通过python代码命中unix命令,但是当我使用subprocess.call函数时,它直接弹出结果。 I want to store that result into a string. 我想将结果存储到字符串中。 Below is my code sample. 下面是我的代码示例。

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

#This returns me 0 #返回我0

I want to store the result in one string and use it for further coding. 我想将结果存储在一个字符串中,并将其用于进一步的编码。

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

This way you can have error and output. 这样您就可以产生错误并输出。

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

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