title: Read from stdin AND forward it to a subprocess in Python
body: <p>I'm writing a wrapper script for a program that optionally accepts input from STDIN. My script needs to process each line of the file, but it also needs to forward STDIN to the program it is wrapping. In minimalist form, this looks something like this:</p>
  
  <pre><code>import subprocess
  import sys
  
  for line in sys.stdin:
      # Do something with each line
      pass
  
  subprocess.call(['cat'])
  </code></pre>
  
  <p>Note that I'm not actually trying to wrap <code>cat</code>, it just serves as an example to demonstrate whether or not STDIN is being forwarded properly.</p>
  
  <p>With the example above, if I comment out the for-loop, it works properly. But if I run it with the for-loop, nothing gets forwarded because I've already read to the end of STDIN. I can't <code>seek(0)</code> to the start of the file because you can't seek on streams.</p>
  
  <p>One possible solution is to read the entire file into memory:</p>
  
  <pre><code>import subprocess
  import sys
  
  lines = sys.stdin.readlines()
  for line in lines:
      # Do something with each line
      pass
  
  p = subprocess.Popen(['cat'], stdin=subprocess.PIPE)
  p.communicate(''.join(lines))
  </code></pre>
  
  <p>which works, but isn't very memory efficient. Can anyone think of a better solution? Perhaps a way to split or copy the stream?</p>
  
  <p>Additional Constraints:</p>
  
  <ol>
      <li>The subprocess can only be called once. So I can't read a line at a time, process it, and forward it to the subprocess.</li>
      <li>The solution must work in Python 2.6</li>
  </ol>