title: How to avoid Python fileinput buffering
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/3670323/setting-smaller-buffer-size-for-sys-stdin">Setting smaller buffer size for sys.stdin?</a>  </p>
  </blockquote>
  
  
  
  <p>I have a Python (2.4/2.7) script using <code>fileinput</code> to read from standard input or from files.  It's easy to use, and works well except for one case:</p>
  
  <pre><code>tail -f log | filter.py
  </code></pre>
  
  <p>The problem is that my script buffers its input, whereas (at least in this case) I want to see its output right away.  This seems to stem from the fact that fileinput uses <code>readlines()</code> to grab up to its <code>bufsize</code> worth of bytes before it does anything.  I tried using a bufsize of 1 and it didn't seem to help (which was somewhat surprising).</p>
  
  <p>I did find that I can write code like this which does not buffer:</p>
  
  <pre><code>while 1:
      line = sys.stdin.readline()
      if not line: break
      sys.stdout.write(line)
  </code></pre>
  
  <p>The problem with doing it this way is that I lose the fileinput functionality (namely that it automatically opens all the files passed to my program, or stdin if none, and it can even decompress input files automatically).</p>
  
  <p>So how can I have the best of both?  Ideally something where I don't need to explicitly manage my input file list (including decompression), and yet which doesn't delay input when used in a "streaming" way.</p>