简体   繁体   中英

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. 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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