I am using Python 3.8.9 and need to create a subprocess that I can send multiple commands to. My code is as follows:
sub1=subprocess.Popen('echo a\n', shell=True, stderr=subprocess.PIPE, stdin=subprocess.PIPE, stdout=subprocess.PIPE, universal_newlines=True)
sub1.stdout.readline()
sub1.stdout.flush()
sub1.stdin.flush()
sub1.stderr.flush()
sub1.stdin.write('echo b\n')
sub1.stdout.readline()
And this prints
'a\n'
7
''
while it should print
'a\n'
7
'b\n'
I don't understand why it seemingly isn't executing the second command, or quite frankly giving any output at all after running another command. I heard some people saying that .write is having issues in Python 3 but that .communicate does work, so I tried that but to no avail
sub1=subprocess.Popen('echo a\n', shell=True, stderr=subprocess.PIPE, stdin=subprocess.PIPE, stdout=subprocess.PIPE, universal_newlines=True)
sub1.stdout.readline()
sub1.stdout.flush()
sub1.stdin.flush()
sub1.stderr.flush()
out, err= sub1.communicate(input='echo b\n', timeout=15)
print(out)
print(err)
Both out and err printed
''
I couldn't find any extant question that covers this issue so I hope it is something that can be simply resolved.