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

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


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

myio.seek(0)


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

#!/usr/bin/env pythonfrom io import BytesIOimport gzipcontent = b"does it work"# write bytes to zip file in memorygzipped_content = Nonewith 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))