title: How can I redirect print output of a function in python
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/1218933/can-i-redirect-the-stdout-in-python-into-some-sort-of-string-buffer">Can I redirect the stdout in python into some sort of string buffer?</a>  </p>
  </blockquote>
  
  
  
  <p>I have a function in python that prints something to the standard output</p>
  
  <pre><code>def foo():
      print("some text")
  </code></pre>
  
  <p>I want to 'redirect' the text that is being printed in this function into a variable, i.e. 'wrap' this function or whatever so that the text is stored in a variable: </p>
  
  <pre><code>text = wrapper(foo)
  </code></pre>
  
  <p>Is there a robust way to temporarily change <code>sys.stdout</code> or to open a variable as a <code>FileObject</code>, or something else?</p>