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

后端 未结 5 1791
春和景丽
春和景丽 2020-12-02 23:35

I\'ve problem to read from Standard input or pipe in python when the pipe is from a \"open\" (do not know right name) file.

I have as example pipetest.py:

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-02 23:59

    Try the next:

    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
    

提交回复
热议问题