title: Save svg to tempfile Python
body: <p>I am using a 3rd party python library that creates <code>.svg</code>'s (specifically for evolutionary trees) which has a <code>render</code> function for tree objects. What I want is the <code>svg</code> in string form that I can edit. Currently I save the <code>svg</code> and read the file as follows:</p>
  
  <pre><code>tree.render('location/filename.svg', other_args...)
  f = open('location/filename.svg', "r")
  svg_string = f.read()
  f.close()
  </code></pre>
  
  <p>This works, but is it possible to use a tempfile instead? So far I have:</p>
  
  <pre><code>t = tempfile.NamedTemporaryFile()
  tmpdir = tempfile.mkdtemp()
  t.name = os.path.join(tmpdir, 'tmp.svg')
  tree.render(t.name, other_args...)
  svg_string = t.read()
  t.close()
  </code></pre>
  
  <p>Can anyone explain why this doesn't work and/or how I could do this without creating a file (which I just have to delete later). The <code>svg_string</code> I go on to edit for use in a django application. </p>
  
  <p><strong>EDIT:</strong> Importantly, the render function can also be used to create other filetypes e.g. <code>.png</code> - so the <code>.svg</code> extension needs to be specified.</p>