subprocess exiting in an incomprehensible fashion

Maciej Dziardziel fiedzia at fiedzia.prv.pl
Fri Oct 21 15:42:36 EDT 2005


Will wrote:

> After the first number is input, the subprocess is exiting after the
> first communicate. I do not understand why.

As help(calc.communicate) says:

Read data from stdout and stderr, until end-of-file is reached.
Wait for process to _terminate_.

> Any help would be much appreciated. I have been hunting quite a bit for
> he answer to no avail.

Simplest solution:

#!/usr/bin/python2.4
import subprocess

calc = subprocess.Popen("dc", stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
max = 5
for value in range(1, max):
    calc.stdin.write("%d\n" % value)
    if value > 1:
        calc.stdin.write("*\n")
calc.stdin.write("p\n")
calc.stdin.write("q\n")
calc.stdin.flush()
status = calc.wait()
print calc.stdout.read()

-- 
Maciej "Fiedzia" Dziardziel (fiedzia (at) fiedzia (dot) prv (dot) pl)
www.fiedzia.prv.pl



More information about the Python-list mailing list