Python default behavior eats all input lines, leaving EOFError for next script

Viewed 40

It appears Python is a greedy taker of input lines?

Very simple Python3 script: testinput.py

for i in range(5):
    x = input()
    print(f'{i}. {x}')

Python script is called repeatedly by Bash script: testinput.sh

python3 testinput.py
python3 testinput.py
python3 testinput.py

Input file: testinput.txt

wo
wajf
wajf
jwfao
ksd
3
34
35
w45
54w
seg
jawfo
34j
serg
se
se
gser
gg
erg
segrse
rg
g
esg
g
g
erg
serg
serg
seg
:wq

Now I feed testinput.txt to testinput.sh:

bash testinput.sh < testinput.txt

And I get:

0. wo
1. wajf
2. wajf
3. jwfao
4. ksd
Traceback (most recent call last):
  File "testinput.py", line 2, in <module>
    x = input()
EOFError: EOF when reading a line
Traceback (most recent call last):
  File "testinput.py", line 2, in <module>
    x = input()
EOFError: EOF when reading a line

How do I make each invocation of python3 take only the input lines it needs? thanks! (Tested in Ubuntu and Cygwin, so unlikely to be platform dependent)

0 Answers
Related