I would like to run an interactive program using python's subprocess. This interactive program starts and runs for sometime. I would like to read the stdout and process it. After running for sometime, it waits with the prompt for user input. This prompt for instance can be "tool_name> ". I wanted to detect this situation and source another script. Here is some basic code, I could come up with. Would be great, if someone can comment further on if this is the correct way to do it.
import subprocess
p = subprocess.Popen(['cli_program' 'a.txt'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.PIPE)
while True:
line = p.stdout.readline()
if not line: #this is true only when application exits
break
elif 'tool_name> ' in line: #wanted to detect the wait state
p.stdin.write('source b.txt\n')
p.stdin.flush()
else:
print line
Here, "cli_program" is my interactive program which will start executing the script a.txt and waits with the prompt "tool_name> ".