title: Python: Capture stdout from subprocess.call
body: <p>I'm trying to do two things when executing a shell cmd with Python:</p>
  
  <ul>
  <li>Capture stdout and print it <em>as it happens</em></li>
  <li>Capture stdout as a whole and process it when the cmd is complete</li>
  </ul>
  
  <p>I looked at <a href="https://docs.python.org/2/library/subprocess.html#subprocess.check_output" rel="nofollow noreferrer"><code>subprocess.check_output</code></a>, but it does not have an stdout param that would allow me to print the output as it happens.</p>
  
  <p>So after reading <a href="https://stackoverflow.com/questions/1996518/retrieving-the-output-of-subprocess-call">this question</a>, I realized I may need to try a different approach.</p>
  
  <pre><code>from subprocess import Popen, PIPE
  
  process = Popen(task_cmd, stdout = PIPE)
  stdout, stderr = process.communicate()
  
  print(stdout, stderr)
  </code></pre>
  
  <p>The problem with this approach is that according to the docs, <a href="https://docs.python.org/2/library/subprocess.html#subprocess.Popen.communicate" rel="nofollow noreferrer">Popen.communicate()</a>:</p>
  
  <blockquote>
    <p>Reads data from stdout and stderr, until end-of-file is reached.
    Wait for process to terminate</p>
  </blockquote>
  
  <p>I still cannot seem to redirect output both to stdout AND to some sort of buffer that can be parsed when the command is complete.</p>
  
  <p>Ideally, I'd like something like:</p>
  
  <pre><code># captures the process output and dumps it to stdout in realtime
  stdout_capture = Something(prints_to_stdout = True)
  process = Popen(task_cmd, stdout = stdout_capture)
  
  # prints the entire output of the executed process
  print(stdout_capture.complete_capture)
  </code></pre>
  
  <p>Is there a recommended way to accomplish this?</p>