pythonpopenpython-2.6fdopen

Problem printing popen stdout from subprocess


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.


Solution

  • 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)