2

I want to write some commands to a python shell and read the output using the Popen code below -

from subprocess import Popen,PIPE

p = Popen(["python"], stdin=PIPE, stdout=PIPE, shell=True)
out, err = p.communicate("help()\n")
print out.rstrip()

But it doesn't print anything I normally see when I run the help() command in python. What am I missing? Note that I'm using python here as an example, I want to communicate with interactive programs in general using python code.

Raminder
  • 1,847
  • 2
  • 18
  • 30
  • 1
    This code works over here … – filmor Nov 18 '13 at 08:26
  • And by "here" I mean on a Linux machine in both Python 2 and 3. – filmor Nov 18 '13 at 08:26
  • 2
    And by this comment I mean that you have to provide more information on your environment. Are you sure python is in your `PATH`? Have you checked whether the process is actually running using `p.poll()`? (see http://stackoverflow.com/questions/2861548/how-to-determine-subprocess-popen-failed-when-shell-true) – filmor Nov 18 '13 at 08:31
  • 1
    Also try printing out the ``err`` variable to see if it has anything of use – stephenfin Nov 18 '13 at 08:36

0 Answers0