1

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.

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
  • 1
    Possible duplicate of [python getoutput() equivalent in subprocess](https://stackoverflow.com/questions/6657690/python-getoutput-equivalent-in-subprocess) – Aran-Fey Sep 20 '18 at 15:52

1 Answers1

0

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)
blhsing
  • 91,368
  • 6
  • 71
  • 106