Reputation: 235
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
Upvotes: 2
Views: 2548
Reputation: 140168
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)
Upvotes: 3
Reputation: 799
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
Upvotes: 0