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

Peter Hansen peter at engcorp.com
Sat Apr 2 10:21:30 EST 2005


Edward C. Jones wrote:
> 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



More information about the Python-list mailing list