title: How to write bytes to a file in Python 3 without knowing the encoding?
body: <p>In Python 2.x with 'file-like' object:</p>
  
  <pre><code>sys.stdout.write(bytes_)
  tempfile.TemporaryFile().write(bytes_)
  open('filename', 'wb').write(bytes_)
  StringIO().write(bytes_)
  </code></pre>
  
  <p>How to do the same in Python 3?</p>
  
  <p>How to write equivalent of this Python 2.x code:</p>
  
  <pre><code>def write(file_, bytes_):
      file_.write(bytes_)
  </code></pre>
  
  <p>Note: <code>sys.stdout</code> is not always semantically a text stream. It might be beneficial to consider it as a stream of bytes sometimes. For example, <a href="http://www.pixelbeat.org/cmdline.html" rel="noreferrer">make encrypted archive of dir/ on remote machine</a>:</p>
  
  <pre><code>tar -c dir/ | gzip | gpg -c | ssh user@remote 'dd of=dir.tar.gz.gpg'
  </code></pre>
  
  <p>There is no point to use Unicode in this case.</p>