[docs] Inaccurate example for the gzip module

Loïc Séguin-Charbonneau loicseguin at gmail.com
Wed Jul 21 04:43:45 CEST 2010


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

First of all, thanks for maintaining great documentation! I use it
constantly, and it is very well written and easy to understand.

I spotted a bug in the gzip documentation for 3.1.2. At the bottom of
the page http://docs.python.org/py3k/library/gzip.html, section 12.2.1.
Examples of usage, we read the following:

import gzip
content = "Lots of content here"
f = gzip.open('/home/joe/file.txt.gz', 'wb')
f.write(content)
f.close()

Running this example fails, as shown below:

(default)Micron:py3k-1.2 loic$ python3
Python 3.1.2 (r312:79147, Jul 18 2010, 02:49:37)
[GCC 4.2.1 (Apple Inc. build 5659)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import gzip
>>> content = "Lots of content here"
>>> f = gzip.open('spotted.txt.gz', 'wb')
>>> f.write(content)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File
"/Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/gzip.py",
line 226, in write
    self.crc = zlib.crc32(data, self.crc) & 0xffffffff
TypeError: must be bytes or buffer, not str
>>>

The solution (which I found thanks to Google and diveintopython3.org) is
to change the fourth line in the example:

import gzip
content = "Lots of content here"
f = gzip.open('/home/joe/file.txt.gz', 'wb')
f.write(content.encode('utf-8'))
f.close()

I haven't check the last example on that webpage, but I think it has the
same problem.

Hope this helps,

Loïc
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkxGXuEACgkQLKWiTxa2C806jQCgwRShTRSnJJwNyoZHwbckgYKe
wrcAnAuFkm1vI1X3FabP/I5btw9IhGMr
=kMgZ
-----END PGP SIGNATURE-----


More information about the docs mailing list