How to use the subprocess Popen.communicate() method?

Viewed 22394

I'm trying to get the standard output of a bash command as a string in Python. Following Popen documentation, I've tried:

import subprocess

p = subprocess.Popen(["echo", "hello"])
stdoutdata, stderrdata = p.communicate()

print stdoutdata

Running this script yields the following output:

hello
None
[Finished in 0.0s]

So although the output is getting printed by Python, the stdoutdata variable is None, and not "hello" as I would like. How can I make it so?

3 Answers
Related