Problem printing popen stdout from subprocess

1.2k views Asked by At

i need put the output command to a variable. I was trying this:

import os
import subprocess

output = subprocess.Popen(["ls", "-l"], stdout=subprocess.PIPE)
print (output.stdout)

output.terminate()

but i get

'open file '<fdopen>', mode 'rb' at 0xb76db5a0>'

what is the problem ? It's okay ?

i use python 2.6.6.

1

There are 1 answers

1
blhsing On BEST ANSWER

output.stdout is a file object. You can use the read method of the file object to get the content of the output:

print(output.stdout.read())

or you can use the Popen.communicate method instead:

stdout, stderr = output.communicate()
print(stdout)