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?
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.
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.
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).
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.
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!')
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.
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.
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!'
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.
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