title: Redirect output from stdin using code module in python?
body: <p>I am trying to figure out a way to redirect output from a script that I'm writing that runs the interactive console.</p>
  
  <p>I need to be able to:</p>
  
  <blockquote>
    <p>capture the output in a string, and then
    check it, or print it out.</p>
  </blockquote>
  
  <p>Is this doable? Should I be using a different python module? </p>
  
  <p>I tried this with the <code>subprocess</code> module, but ran into a problem where I could only do 1 read at the end of the process. That won't work, I need to read multiple times from stdout. This is what I have so far:</p>
  
  <pre><code>import code
  console = code.InteractiveConsole()
  console.push('from sim import newsteam\n')
  console.push('assembler = newsteam()\n')
  console.push('assembler.__init__()\n')
  
  line = console.push('assembler.reportr()\n')
  #I need to be able to interact with this output
  line1 = console.push('assembler.reportpc()\n')
  line2 = console.push('assembler.reportm()\n')
  
  print("this is the result: " + line + line1 + line2)
  </code></pre>