I'm running the following script with Python 3:
import sys, subprocess
line = sys.stdin.readline()
print(f'[p] {line}', end='')
subprocess.run('read one; echo "[s] $one"', shell=True)
line = sys.stdin.readline()
print(f'[p] {line}', end='')
If I run this script normally, this happens, as I expected:
a
[p] a
b
[s] b
c
[p] c
(I've typed three lines, with a single letter on each). However, if I create a text file containing these three lines, and run it using python3 script.py < file.txt, I get the following output:
[p] a
[s]
[p] b
When I expect to get
[p] a
[s] b
[p] c
I'm fairly sure this is due to an stdin buffering issue. I've tried (to no avail) setting PYTHONUNBUFFERED, and running Python with -u. Can I disable stdin buffering? Or can I access the contents of the buffer to pass them to my shell command?
Thanks!