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

nic picture nic · Dec 2, 2016 · Viewed 11.5k times · Source

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

Answer

Nagalakshmi Srirama picture Nagalakshmi Srirama · Oct 24, 2017

you can convert it to bytes like this.

import gzip 
with gzip.open(filename, 'wb') as fd:
   fd.write('blah blah blah'.encode('utf-8'))