title: How do i extract and save a base64 byte array from an xml document in python
body: <p>I have an XML file that contains images stored as base64 byte arrays.</p>
  <p>I want to extract the arrays and save as image files</p>
  <p>I can get the array from the XML as a string like so</p>
  <pre><code> photo=record.find('Photo').text.tobase64
  </code></pre>
  <p>but I cannot work out how to output the image</p>
  <pre><code> picpath = '..\\pics\\' + upn +'.jpg'
  f = open(picpath, 'wb')
  f.write(photo)
  f.close()
  </code></pre>
  <p>complains about the photo being a string.</p>
  <p>I am obviously missing something, but I cannot work out what</p>
  <p>EDIT</p>
  <p>the error</p>
  <p>----&gt; 7     f.write(photo)
  8     f.close()
  9</p>
  <p>TypeError: a bytes-like object is required, not 'str'</p>
  <p>and the XML (a truncated version of the byte array though)</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; standalone=&quot;yes&quot;?&gt; 
  &lt;Record&gt; &lt;Photo&gt;/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQ&lt;/Photo&gt;
   &lt;ID&gt;E9282&lt;/ID&gt; 
  &lt;/Record&gt; 
  </code></pre>