How do you read from stdin in python from a pipe which has no ending

Try this:

import sys
import time
k = 0
try:
    buff=""
    while True:
        buff += sys.stdin.read(1)
        if buff.endswith('\n'):
            print buff[:-1]
            buff=""
            k = k + 1
except KeyboardInterrupt:
   sys.stdout.flush()
   pass
print k

Leave a Comment