Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid buffering in the Python fileinput library

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

  • python -u does not work for stdin in Python 3.
  • sys.stdin = sys.stdin.detach() throws a ValueError: underlying buffer has been detached.
  • None of these work for non-stdin inputs and other files being used as stream.
  • Adding a hook does not work: FileInput(openhook=hook_nobuf) and using open(buffering=0) in the hook.

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.

like image 802
dpyro Avatar asked Feb 21 '13 21:02

dpyro


1 Answers

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!")
like image 103
dpyro Avatar answered Oct 27 '22 19:10

dpyro