I want to write a file. Based on the name of the file this may or may not be compressed with the gzip
module. Here is my code:
import gzip
filename = 'output.gz'
opener = gzip.open if filename.endswith('.gz') else open
with opener(filename, 'wb') as fd:
print('blah blah blah'.encode(), file=fd)
I'm opening the writable file in binary mode and encoding my string to be written. However I get the following error:
File "/usr/lib/python3.5/gzip.py", line 258, in write
data = memoryview(data)
TypeError: memoryview: a bytes-like object is required, not 'str'
Why is my object not a bytes? I get the same error if I open the file with 'w'
and skip the encoding step. I also get the same error if I remove the '.gz'
from the filename.
I'm using Python3.5 on Ubuntu 16.04
But the same code when executed in Python 3 will throw the error - typeerror: a bytes-like object is required, not 'str'. This is because, in Python 2, the strings are by default treated as bytes.
From python 3 gzip doc-quoting: "... The mode argument can be any of 'r', 'rb', 'a', 'ab', 'w', 'wb', 'x' or 'xb' for binary mode, or 'rt', 'at', 'wt', or 'xt' for text mode ..."
But the same code when executed in Python 3 will throw the error - typeerror: a bytes-like object is required, not 'str'. This is because, in Python 2, the strings are by default treated as bytes. The original strings in Python 2 are 8-bit strings, which play a crucial role while working with byte sequences and ASCII text.
Make sure that you open up any text files in text read mode instead of binary read mode. The error “typeerror: a bytes-like object is required, not ‘str’” is raised when you treat an object as a string instead of as a series of bytes. A common scenario in which this error is raised is when you read a text file as a binary.
For me, changing the gzip flag to 'wt' did the job. I could write the original string, without "byting" it. (tested on python 3.5, 3.7 on ubuntu 16).
From python 3 gzip doc - quoting: "... The mode argument can be any of 'r', 'rb', 'a', 'ab', 'w', 'wb', 'x' or 'xb' for binary mode, or 'rt', 'at', 'wt', or 'xt' for text mode..."
import gzip
filename = 'output.gz'
opener = gzip.open if filename.endswith('.gz') else open
with opener(filename, 'wt') as fd:
print('blah blah blah', file=fd)
!zcat output.gz
> blah blah blah
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With