Question

I have been (unsuccessfully) trying to use Python's subprocess module to interact with an executable program. The program is a very simple command line based script.

It basically just acts in the following way: prompt user with text, wait for numeric input, prompt with more text, wait for next input, etc.

So I set up the subprocess like so

from subprocess import Popen, PIPE
p = Popen('filename.exe', stdin=PIPE, stdout=PIPE)

Then I get the first prompt

print p.stdout.readline()

Properly returns

Enter some value blah blah

Great! Then I try to enter the desired value

p.stdin.write('10.0')

It then completely hangs. I can try grabbing the next prompt

print p.stdout.readline()

but it still hangs no matter what.

What is the proper way to do this one line read/write business? I must be messing up the write line I think.

Was it helpful?

Solution

You are probably forgetting to output a newline:

p.stdin.write('10.0\n')

What happens is that your subprocess is receiving the data, but waiting for more input, until it finds a newline. If you wait for output from the process in this state, you deadlock the system.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top