Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binary buffer in Python

In Python you can use StringIO for a file-like buffer for character data. Memory-mapped file basically does similar thing for binary data, but it requires a file that is used as the basis. Does Python have a file object that is intended for binary data and is memory only, equivalent to Java's ByteArrayOutputStream?

The use-case I have is I want to create a ZIP file in memory, and ZipFile requires a file-like object.

like image 461
jelovirt Avatar asked Aug 24 '08 15:08

jelovirt


People also ask

What is binary buffer?

What are Buffers? Binary is simply a set or a collection of 1 and 0 . Each number in a binary, each 1 and 0 in a set are called a bit. Computer converts the data to this binary format to store and perform operations.

What is a buffer in Python?

In Python, the buffer type object is used to show the internal data of a given object in a byte-oriented format. Python's main use of buffers is storing and manipulating huge data arrays and processing them without creating copies.

What is binary format in Python?

"Binary" files are any files where the format isn't made up of readable characters. Binary files can range from image files like JPEGs or GIFs, audio files like MP3s or binary document formats like Word or PDF. In Python, files are opened in text mode by default.


2 Answers

You are probably looking for io.BytesIO class. It works exactly like StringIO except that it supports binary data:

from io import BytesIO bio = BytesIO(b"some initial binary data: \x00\x01") 

StringIO will throw TypeError:

from io import StringIO sio = StringIO(b"some initial binary data: \x00\x01") 
like image 112
akhan Avatar answered Sep 28 '22 10:09

akhan


As long as you don't try to put any unicode data into your StringIO and you are careful NOT to use cStringIO you should be fine.

According to the StringIO documentation, as long as you keep to either unicode or 8-bits everything works as expected. Presumably, StringIO does something special when someone does a f.write(u"asdf") (which ZipFile does not do, to my knowledge). Anyway;

import zipfile import StringIO  s = StringIO.StringIO() z = zipfile.ZipFile(s, "w") z.write("test.txt") z.close() f = file("x.zip", "w") f.write(s.getvalue()) s.close() f.close() 

works just as expected, and there's no difference between the file in the resulting archive and the original file.

If you know of a particular case where this approach does not work, I'd be most interested to hear about it :)

like image 39
Henrik Gustafsson Avatar answered Sep 28 '22 11:09

Henrik Gustafsson