Module subprocess: How to "communicate" more than once?

Edward C. Jones edcjones at comcast.net
Fri Apr 1 20:06:33 EST 2005


I have a program named "octave" (a Matlab clone). It runs in a terminal, 
types a prompt and waits for the user to type something. If I try

# Run octave.
oct = subprocess.Popen("octave", stdin=subprocess.PIPE)

# Run an octave called "startup".
oct.communicate("startup")

# Change directory inside octave.
oct.communicate("cd /home/path/to/my/dir")

I get:

Traceback (most recent call last):
   File "./popen.py", line 29, in ?
     oct.communicate("cd /home/path/to/my/dir")
   File "/usr/local/lib/python2.4/subprocess.py", line 1044, in communicate
     self.stdin.flush()
ValueError: I/O operation on closed file

How do I set up a subprocess so I can send it a command and get the 
answer, then send it another command and get an answer, etc.?



More information about the Python-list mailing list