Search code examples
pythonsubprocesspython-2.5

How to convert subprocess.popen.object to normal string in python 2.5?


I am executing below code which is working fine but the ouput it gives is as "subprocess.Popen object at 0x206"

import sys
from subprocess import Popen 
var1= "I am testing"
Process=Popen('/home/tester/Desktop/test.sh %s' % (str(var1),), shell=True)
print Process

returns: subprocess.Popen object at 0x206

Also test.sh just echos given variable all it's having is:

#!/bin/bash 
echo $1 
echo $2

Unfortunately I cannot upgrade my application python version from 2.5 hence not able to use the current subprocess features such as check_ouput, I am looking for an solution by which I can convert the given output into normal string?


Solution

  • You need to pass stdout and stderr to Popen and read them:

    proc = subprocess.Popen('/home/tester/Desktop/test.sh %s' % (str(var1),), stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=True)
    out, err = proc.communicate()  # Read data from stdout and stderr
    
    print out
    print err