title: What is the best way to write the contents of a StringIO to a file?
body: <p>What is the best way to write the contents of a <code>StringIO</code> buffer to a file ?</p>
  <p>I currently do something like:</p>
  <pre><code>buf = StringIO()
  fd = open('file.xml', 'w')
  # populate buf
  fd.write(buf.getvalue ())
  </code></pre>
  <p>But then <code>buf.getvalue()</code> would make a copy of the contents?</p>