문제

I've seen this question asked here, but the answers given did not work in my case and was marked duplicate.

I dug in the source code (/usr/lib/python3.2/fileinput.py) and saw that readlines(bufsize) was being used internally to load a buffer. No shell or other piping shenanigans.

도움이 되었습니까?

해결책

What worked for me was simply setting FileInput(bufsize=1). The file.readlines() documentation does state "The optional size argument, if given, is an approximate bound on the total number of bytes in the lines returned." In practice, I get exactly one new line every time rather than having to fill a buffer.

with fileinput.input(bufsize=1) as f:
    for line in f:
        print("One line in, one line out!")
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top