title: Write files to disk with python 3.x
body: <p>Using BottlePy, I use the following code to upload a file and write it to disk :</p>
  
  <pre><code>upload = request.files.get('upload')
  raw = upload.file.read()
  filename = upload.filename
  with open(filename, 'w') as f:
      f.write(raw)
  return "You uploaded %s (%d bytes)." % (filename, len(raw))
  </code></pre>
  
  <p>It returns the proper amount of bytes every single time.</p>
  
  <p>The upload works fine for file like <code>.txt</code>, <code>.php</code>, <code>.css</code> ...</p>
  
  <p>But it results in a corrupted file for other files like <code>.jpg</code>, <code>.png</code>, <code>.pdf</code>, <code>.xls</code> ...</p>
  
  <p>I tried to change the <code>open()</code> function</p>
  
  <pre><code>with open(filename, 'wb') as f:
  </code></pre>
  
  <p>It returns the following error:</p>
  
  <blockquote>
    <p>TypeError('must be bytes or buffer, not str',)</p>
  </blockquote>
  
  <p>I guess its an issue related to binary files ?</p>
  
  <p>Is there something to install on top of Python to run upload for any file type ?</p>
  
  <hr>
  
  <p>Update</p>
  
  <p>Just to be sure, as pointed out by @thkang I tried to code this using the dev version of bottlepy and the built-in method .save()</p>
  
  <pre><code>upload = request.files.get('upload')
  upload.save(upload.filename)
  </code></pre>
  
  <p>It returns the exact same Exception error</p>
  
  <pre><code>TypeError('must be bytes or buffer, not str',)
  </code></pre>
  
  <hr>
  
  <p>Update 2</p>
  
  <p>Here the final code which "works" (and dont pop the error <code>TypeError('must be bytes or buffer, not str',)</code></p>
  
  <pre><code>upload = request.files.get('upload')
  raw = upload.file.read().encode()
  filename = upload.filename
  with open(filename, 'wb') as f:
      f.write(raw)
  </code></pre>
  
  <p>Unfortunately, the result is the same : every <code>.txt</code> file works fine, but other files like <code>.jpg</code>, <code>.pdf</code> ... are corrupted</p>
  
  <p>I've also noticed that those file (the corrupted one) have a larger size than the orginal (before upload)</p>
  
  <p>This binary thing must be the issue with Python 3x</p>
  
  <p>Note :</p>
  
  <ul>
  <li><p>I use python 3.1.3</p></li>
  <li><p>I use BottlePy 0.11.6 (<a href="https://github.com/defnull/bottle/blob/master/bottle.py" rel="nofollow">raw bottle.py file</a>, no 2to3 on it or anything)</p></li>
  </ul>