0





16

Advertisement

I have two scripts one just asks for input then prints the input. The other one launches the script. It is only half working and I was hoping someone could shed some light on the issue.

test.py

import subprocess

process = subprocess.Popen('python test2.py', shell=False,
                           stdin=subprocess.PIPE, stdout=subprocess.PIPE,
                           stderr=None)
process.stdin.write("test\r\n".encode('utf-8'))
process.stdin.flush()

result = process.stdout.readline()[:-2].decode('utf-8')
print(result)
while result != "":
    result = process.stdout.readline()[:-2].decode('utf-8')
    print(result)

test2.py

s=input("input here:")
print(s)

expected output:

input here:test
test

actual output:

input here:test
Question author Kaminsknator | Source

Advertisement


0


Note that the input is not echoed. If you are sitting at a terminal and were to run 'python test2.py', the terminal would echo your input as you typed it. That would show the word "test" twice. Your test.py program sends the word "test" through the pipe; it does not print it to stdout. The test2.py program prints the input once. Thus the output "input here:test" is correct.

Answer author Dsh