I basically want to do exactly whats in the documentation of gzip.GzipFile
:
Calling a GzipFile object’s close() method does not close fileobj, since you might wish to append more material after the compressed data. This also allows you to pass a io.BytesIO object opened for writing as fileobj, and retrieve the resulting memory buffer using the io.BytesIO object’s getvalue() method.
With a normal file object it works as expected.
>>> import gzip
>>> fileobj = open("test", "wb")
>>> fileobj.writable()
True
>>> gzipfile = gzip.GzipFile(fileobj=fileobj)
>>> gzipfile.writable()
True
But I can't manage to get a writable gzip.GzipFile
object when passing a io.BytesIO
object.
>>> import io
>>> bytesbuffer = io.BytesIO()
>>> bytesbuffer.writable()
True
>>> gzipfile = gzip.GzipFile(fileobj=bytesbuffer)
>>> gzipfile.writable()
False
Do I have to open the io.BytesIO
explicit for writing, and how would I do so? Or is there a difference between a file object returned by open(filename, "wb")
and a object returned by io.BytesIO()
I didn't think of?