How to let the output being printed on the interactive shell with subprocess
>>> import subprocess
>>> subprocess.Popen("pwd")
<subprocess.Popen object at 0xa7692cc>
>>> subprocess.call(["ls", "-l"])
0
I have tried the above command in Python interactive shell and expect to see the output inside the shell environment. However, it开发者_开发知识库 ends up with just some return values.
What should I do in order to let the return results printed inside the shell?
It'd help to read the documentation first.
p = subprocess.Popen('pwd', stdout = subprocess.PIPE)
p.communicate() # returns (stdout, None)
Cat Plus Plus is right, however, if you prefer the call
function, you can also do this like so:
import subprocess
import sys
subprocess.call('pwd', stdout=sys.stdout) # outputs results of `pwd` to stdout
精彩评论