Using Python, I want to execute an external program, interact with it by providing text to its standard input, and then print the result.
As an example, I created the following external program, called test.py:
print('Test Program')
print('1 First option, 2 Second Option')
choice = input()
if choice == '1':
second_param = input('Insert second param: ')
result = choice + ' ' + second_param
print(result)
If I run this program directly, it works as expected. If I provide the input 1 and then 2, the output is 1 2.
I want to run this program from another script and interact with it to print the same result.
After reading the documentation for subprocess, I ended up with the following:
EXTERNAL_PROG = 'test.py'
p = Popen(['py', EXTERNAL_PROG], stdout=PIPE, stdin=PIPE, shell=True)
print(p.stdout.readline().decode('utf-8'))
print(p.stdout.readline().decode('utf-8'))
p.stdin.write(b'1\n')
p.stdin.write(b'2\n')
print(p.stdout.readline().decode('utf-8'))
However, when I run the code, the program freezes after printing 1 First option, 2 Second Option, and I need to restart my shell. This is probably caused by the fact that subprocess.stdout.readline() expects to find a newline character, and the prompt for the second param doesn’t contain one.
I found two related questions, but I still couldn’t get it to work.
Here, the answer recommends using the pexpect module. I tried to adapt the code to my situation, but it didn’t work.
Here, the suggestion is to use -u, but adding it didn’t change anything.
I know that a solution can be found by modifying test.py, but this is not possible in my case since I need to use another external program. This is just a minimal example based on it.
p.stdin.readline()commands top.stdin.read(). You may even want to put it in a loop, aswhile (print(p.stdin.read())): nextto ensure that anything that needs reading gets read. Not sure if that's the issue, but it will test your theory about the readline() functionality.