I want to write a file. Based on the file name, this may or may not be compressed using 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 open the recording file in binary mode and encode my string for writing. 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 byte? 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 file name.
I am using Python3.5 on Ubuntu 16.04
source share