title: Redirect an output command to a variable or file?
body: <p>I'm trying to write a python script that will allow me to take the output from a command and to put that into a file or variable (Preferability  a variable).</p>
  
  <p>In my code, I have redirected the output to a <code>StringIO()</code> object. From that, I want take the output a command and to put it into that <code>StringIO()</code> object.</p>
  
  <p>Here is a sample of my code:</p>
  
  <pre><code>from StringIO import StringIO
  import sys
  
  old_stdout = sys.stdout
  
  result = StringIO()
  sys.stdout = result
  
  # This will output to the screen, and not to the variable
  # I want this to output to the 'result' variable
  os.system('ls -l')
  </code></pre>
  
  <p>Also, how do I take result and put it into a string?</p>
  
  <p>Thanks in advance!!</p>