diff --git a/files.html b/files.html index 14f013c..326eb72 100644 --- a/files.html +++ b/files.html @@ -414,16 +414,22 @@ AttributeError: '_io.BufferedReader' object has no attribute 'encoding'you@localhost:~$ python3 >>> import gzip ->>> with gzip.open('out.log.gz', mode='wb') as z_file: +>>> with gzip.open('out.log.gz', mode='wb') as z_file: ... z_file.write('A nine mile walk is no joke, especially in the rain.'.encode('utf-8')) ... >>> exit() -you@localhost:~$ ls -l out.log.gz +you@localhost:~$ ls -l out.log.gz -rw-r--r-- 1 mark mark 79 2009-07-19 14:29 out.log.gz -you@localhost:~$ gunzip out.log.gz -you@localhost:~$ cat out.log +you@localhost:~$ gunzip out.log.gz +you@localhost:~$ cat out.log A nine mile walk is no joke, especially in the rain. +
    +
  1. You should always open gzipped files in binary mode. (Note the 'b' character in the mode argument.) +
  2. I constructed this example on Linux. If you’re not familiar with the command line, this command is showing the “long listing” of the gzip-compressed file you just created in the Python Shell. This listing shows that the file exists (good), and that it is 79 bytes long. That’s actually larger than the string you started with! The gzip file format includes a fixed-length header that contains some metadata about the file, so it’s inefficient for extremely small files. +
  3. The gunzip command (pronounced “gee-unzip”) decompresses the file and stores the contents in a new file named the same as the compressed file but without the .gz file extension. +
  4. The cat command displays the contents of a file. This file contains the string you originally wrote directly to the compressed file out.log.gz from within the Python Shell. +