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

PythonByteGzipBytesio

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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

myio.seek(0)

Solution 2 - Python

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))

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontwasbrilligView Question on Stackoverflow
Solution 1 - PythonmgilsonView Answer on Stackoverflow
Solution 2 - PythonGatsby LeeView Answer on Stackoverflow