Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python3 write gzip file - memoryview: a bytes-like object is required, not 'str'

Tags:

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

like image 420
nic Avatar asked Dec 02 '16 00:12

nic


People also ask

Why does Python 3 require a bytes-like object instead of a string?

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.

What is the mode argument in Python 3 gzip?

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 ..."

Why does Python 3 throw TypeError-bytes-like object is required?

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.

How do I fix TypeError bytes-like object is required?

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.


1 Answers

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
like image 78
mork Avatar answered Oct 10 '22 16:10

mork