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

  • Thread starter Edward C. Jones
  • Start date
E

Edward C. Jones

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.?
 
P

Peter Hansen

Edward said:
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:
ValueError: I/O operation on closed file

As defined in the docs. You can't use communicate()
for what you're trying to do.
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.?

communicate is not the only way of communicating... use
the stdin/stdout handles that are provided. You can
very likely find examples of usage in the list archives.

-Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top