title: Saving zip file in python to disk
body: <p>I have a <code>bytea</code> object in a database which is a zip file, I can retrieve it and read it as a zip file, but I also want to save it to disk.</p>
  <p>I can't work out how to write <code>zf_model</code> to disk. I've also tried <code>zf.write(io.BytesIO(model.model_file))</code>, i.e. not converting to zip first, but that doesn't work either.</p>
  <p>This is what I've tried:</p>
  <pre class="lang-py prettyprint-override"><code>from zipfile import ZipFile
  from io import BytesIO
          
  #Retrieve the zip file from database (zip file is in a field called model_file for object: Model)
  model = Model().query.filter_by(param = &quot;test&quot;).first()
  #convert the retrieved object to a zip file
  zf_model = ZipFile(BytesIO(model.model_file), &quot;w&quot;)
      
  tempfile = &quot;/tmp/test.zip&quot;
  
  with zipfile.ZipFile(tempfile, &quot;w&quot;, compression=zipfile.ZIP_DEFLATED) as zf:
      zf.write(zf_model)
  
  </code></pre>
  <p>Gives error:</p>
  <pre class="lang-sh prettyprint-override"><code>TypeError: a bytes-like object is required, not 'ZipFile'
  </code></pre>
  <p>Trying to write the <code>bytea</code> object directly</p>
  <pre class="lang-py prettyprint-override"><code>with open(tempfile, 'w+') as f:
      f.write(model.model_file)      
  </code></pre>
  <p>gives error:</p>
  <pre class="lang-sh prettyprint-override"><code>TypeError: write() argument must be str, not bytes
  </code></pre>