Writing then reading in-memory bytes (BytesIO) gives a blank result
Asked Answered
D

3

119

I wanted to try out the python BytesIO class.

As an experiment I tried writing to a zip file in memory, and then reading the bytes back out of that zip file. So instead of passing in a file-object to gzip, I pass in a BytesIO object. Here is the entire script:

from io import BytesIO
import gzip

# write bytes to zip file in memory
myio = BytesIO()
with gzip.GzipFile(fileobj=myio, mode='wb') as g:
    g.write(b"does it work")

# read bytes from zip file in memory
with gzip.GzipFile(fileobj=myio, mode='rb') as g:
    result = g.read()

print(result)

But it is returning an empty bytes object for result. This happens in both Python 2.7 and 3.4. What am I missing?

Drayage answered 12/11, 2014 at 5:36 Comment(0)
D
204

You need to seek back to the beginning of the file after writing the initial in memory file...

myio.seek(0)
Demonism answered 12/11, 2014 at 5:40 Comment(3)
Buffers filled by matplotlib savefig() also do need this before they can be sent by an application server. Thanks for ending hours of research!Vachon
Or use getvalue(). These little things are scattered all over the place!Epicotyl
getvalue() didn't work for me except the seek(0)Oystercatcher
F
7

How about we write and read gzip content in the same context like this?

#!/usr/bin/env python

from io import BytesIO
import gzip

content = b"does it work"

# write bytes to zip file in memory
gzipped_content = None
with BytesIO() as myio:
    with gzip.GzipFile(fileobj=myio, mode='wb') as g:
        g.write(content)
    gzipped_content = myio.getvalue()

print(gzipped_content)
print(content == gzip.decompress(gzipped_content))
Foch answered 26/6, 2019 at 16:21 Comment(3)
I don't think g.close() is needed here, right?Cattycornered
@Cattycornered you're right. since it is in context, g.close() is not necessary. I removed it. Thank you.Foch
How does this solution expand for multiple rows of content such as parsing a csv for example?Lucre
S
4

myio.getvalue() is an alternative to seek that returns the bytes containing the entire contents of the buffer (docs).

It worked for me after facing a similar issue.

Sr answered 14/9, 2022 at 17:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.