Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

In Python 2.x with 'file-like' object:

sys.stdout.write(bytes_) tempfile.TemporaryFile().write(bytes_) open('filename', 'wb').write(bytes_) StringIO().write(bytes_) 

How to do the same in Python 3?

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

def write(file_, bytes_):     file_.write(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 user@remote 'dd of=dir.tar.gz.gpg' 

There is no point to use Unicode in this case.

like image 666
jfs Avatar asked Nov 27 '10 08:11

jfs


People also ask

How do you write bytes into data in Python?

First, open a file in binary write mode and then specify the contents to write in the form of bytes. Next, use the write function to write the byte contents to a binary file.

How do you write bytes of a string in Python?

If you want the size of the string in bytes, you can use the getsizeof() method from the sys module.


2 Answers

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

sys.stdout.buffer.write(bytes_) 

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

This accesses the underlying byte buffer.

tempfile.TemporaryFile().write(bytes_) 

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().write(bytes_) 

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.

like image 175
Matthew Flaschen Avatar answered Sep 18 '22 04:09

Matthew Flaschen


Specify binary mode, 'b', when you open your file:

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

https://docs.python.org/3.3/library/functions.html#open

like image 30
bigonazzi Avatar answered Sep 18 '22 04:09

bigonazzi