dahko37
dahko37

Reputation: 181

Pressing Ctrl-D doesn't send EOF to my python program

I made some code in python to read stdin. When I execute it and input all the data I want I press Ctrl-D, which should send EOF macro but in reality nothing happens. Im on arch-linux using alacritty terminal and zsh shell if thats relevant.

from sys import stdin, stdout


bl = False
res = ""
for line in stdin:
    while line:
        if bl:
            bl = False
            arr = list(map(int, line.strip().split(" ")))
            dicc = {arr.index(x) : x for x in arr}
            for key, value in dicc.items():
                res += "{}:{} ".format(key, value)
        else:
            bl = True
stdout.write(res)

Upvotes: 0

Views: 1954

Answers (1)

fanta fles
fanta fles

Reputation: 385

from sys import stdin, stdout

res = ''

while True:
    line =  stdin.readline()
    if line:
        res += line #do calculations
    else:
        break

stdout.write(res)

or using the walrus operator from python 3.8:

while True:
    if line := stdin.readline():
        pass # do calculations with line
    else:
        break

This will keep getting values from stdin until ctrl+d is pressed

Upvotes: 1

Related Questions