Saturday, 15 August 2015

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



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

i wanted seek out python bytesio class.

as experiment tried writing zip file in memory, , reading bytes out of zip file. instead of passing in file-object gzip, pass in bytesio object. here entire script:

from io import bytesio import gzip # write bytes zip file in memory myio = bytesio() g = gzip.gzipfile(fileobj=myio, mode='wb') g.write(b"does work") g.close() # read bytes zip file in memory g = gzip.gzipfile(fileobj=myio, mode='rb') result = g.read() g.close() print(result)

but returning empty bytes object result. happens in both python 2.7 , 3.4. missing?

you need seek origin of file after writing initial in memory file...

myio.seek(0)

python byte gzip bytesio

No comments:

Post a Comment