2

I'm unable to get this to work

import gzip
content = "Lots of content here"
f = gzip.open('file.txt.gz', 'a', 9)
f.write(content)
f.close()

I get output:

============== RESTART: C:/Users/Public/Documents/Trial_vr06.py ==============
Traceback (most recent call last):
  File "C:/Users/Public/Documents/Trial_vr06.py", line 4, in <module>
    f.write(content)
  File "C:\Users\SONID\AppData\Local\Programs\Python\Python36\lib\gzip.py", line 260, in write
    data = memoryview(data)
TypeError: memoryview: a bytes-like object is required, not 'str'

This was linked in an answer Python Gzip - Appending to file on the fly to Is it possible to compress program output within python to reduce output file's size?

I've tried integer data but no effect. What is the issue here

DS R
  • 235
  • 2
  • 13

2 Answers2

3

by default gzip streams are binary (Python 3: gzip.open() and modes).

No problem in Python 2, but Python 3 makes a difference between binary & text streams.

So either encode your string (or use b prefix if it's a literal like in your example, not always possible)

f.write(content.encode("ascii"))

or maybe better for text only: open the gzip stream as text:

f = gzip.open('file.txt.gz', 'at', 9)

note that append mode on a gzip file works is not that efficient (Python Gzip - Appending to file on the fly)

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
0

In order to compress your string, it must be a binary value. In order to do that simply put a "b" in front of your string. This will tell the interpreter to read this as the binary value and not the string value.

content = b"Lots of content here"

https://docs.python.org/3/library/gzip.html

zachbugay
  • 704
  • 1
  • 9
  • 21
  • Thanks for the answer. Could it cause some changes when I later read this data from the output file a program? – DS R Oct 12 '17 at 12:04
  • 1
    All you'll need to do is set the flag as "rb" or some other form denoting binary and you'll be set without any issues. – zachbugay Oct 12 '17 at 12:05