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

1
  • This link has the same question. Commented May 23, 2019 at 20:45

2 Answers 2

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)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Is this a change only for Py3.x?
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

2 Comments

Thanks for the answer. Could it cause some changes when I later read this data from the output file a program?
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.