How to write bytes to a file in Python 3 without knowing the encoding?

Python Programming

Question or problem about Python programming:

In Python 2.x with ‘file-like’ object:

open('filename', 'wb').write(bytes_)

How to do the same in Python 3?

How to write equivalent of this Python 2.x code:

def write(file_, bytes_):

Note: sys.stdout is not always semantically a text stream. It might be beneficial to consider it as a stream of bytes sometimes. For example, make encrypted archive of dir/ on remote machine:

tar -c dir/ | gzip | gpg -c | ssh [email protected] 'dd of=dir.tar.gz.gpg'

There is no point to use Unicode in this case.

How to solve the problem:

Solution 1:

It’s a matter of using APIs that operate on bytes, rather than strings.


As the docs explain, you can also detach the streams, so they’re binary by default.

This accesses the underlying byte buffer.


This is already a byte API.

open('filename', 'wb').write(bytes_)

As you would expect from the ‘b’, this is a byte API.

from io import BytesIO

BytesIO is the byte equivalent to StringIO.

EDIT: write will Just Work on any binary file-like object. So the general solution is just to find the right API.

Solution 2:

Specify binary mode, ‘b’, when you open your file:

with open('myfile.txt', 'wb') as w:

Hope this helps!