Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing text to gzip file

Following tutorials and examples found in blogs and in other threads here, it appears that the way to write to a .gz file is to open it in binary mode and write the string as is:

import gzip
with gzip.open('file.gz', 'wb') as f:
    f.write('Hello world!')

I tried it, and got the following exception:

  File "C:\Users\Tal\Anaconda3\lib\gzip.py", line 258, in write
    data = memoryview(data)
TypeError: memoryview: a bytes-like object is required, not 'str'

So I tried opening the file in text mode:

import gzip
with gzip.open('file.gz', 'w') as f:
    f.write('Hello world!')

But I got the same error:

  File "C:\Users\Tal\Anaconda3\lib\gzip.py", line 258, in write
    data = memoryview(data)
TypeError: memoryview: a bytes-like object is required, not 'str'

How can this problem be addressed in Python3?

like image 435
AgvaniaRekuva Avatar asked Mar 14 '18 19:03

AgvaniaRekuva


People also ask

How do I create a gzip file?

gz file is a Tar archive compressed with Gzip. To create a tar. gz file, use the tar -czf command, followed by the archive name and files you want to add.

How do you write gzip in Python?

gz file is to open it in binary mode and write the string as is: import gzip with gzip. open('file. gz', 'wb') as f: f.

What is the difference between ZIP and gzip?

The most important difference is that gzip is only capable to compress a single file while zip compresses multiple files one by one and archives them into one single file afterwards. Thus, gzip comes along with tar most of the time (there are other possibilities, though).

What does it mean to gzip a file?

GZIP, short for GNU Zip, is a compression/decompression format developed as part of a larger project to create a free software alternative to UNIX in the 1980s. This open source compression format does not support archiving, so it is used to compress single files. GZIP produces zipped files with the . gz extension.

How to write a string to a gzip file?

Following tutorials and examples found in blogs and in other threads here, it appears that the way to write to a .gz file is to open it in binary mode and write the string as is: import gzip with gzip.open ('file.gz', 'wb') as f: f.write ('Hello world!')

What does gzip do?

With no arguments, gzip compresses the standard input and writes the compressed file to standard output. ZIP and GZIP are two very popular methods of compressing files, in order to save space, or to reduce the amount of time needed to transmit the files across the network, or internet.

How do I write asynchronously to a gzip stream?

Use the WriteAsync method to write asynchronously to the current stream. If the write operation is successful, the position within the GZip stream advances by the number of bytes written. If an exception occurs, the position within the GZip stream remains unchanged.

How do I write Hello world in gzip format?

with gzip.open ('file.gz', 'wt') as f: f.write ('Hello world!') The documentation has a couple of handy examples on usage. Instead of 'Hello world!'.encode () simply write b'Hello world!'


1 Answers

mode='wb'

When writing to a file opened in binary mode, you must write bytes, not string. Encode your string using str.encode:

with gzip.open('file.gz', 'wb') as f:
    f.write('Hello world!'.encode())

mode='wt'

(found by OP) Alternatively, you can write strings to your file when you open it in the wt (explicit text) mode:

with gzip.open('file.gz', 'wt') as f:
    f.write('Hello world!')

The documentation has a couple of handy examples on usage.

like image 54
cs95 Avatar answered Oct 24 '22 09:10

cs95