diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2010-08-17 21:15:19 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2010-08-17 21:15:19 (GMT) |
commit | 38a4f7a38df4bba924ec9f1959beef0c9347c5da (patch) | |
tree | 9d10133f505021a90158af4f4c713dae51217b4a /Doc | |
parent | f72006f4429975a9d221e046e43dabd4f41eda23 (diff) | |
download | cpython-38a4f7a38df4bba924ec9f1959beef0c9347c5da.zip cpython-38a4f7a38df4bba924ec9f1959beef0c9347c5da.tar.gz cpython-38a4f7a38df4bba924ec9f1959beef0c9347c5da.tar.bz2 |
Merged revisions 84156 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
........
r84156 | antoine.pitrou | 2010-08-17 23:11:49 +0200 (mar., 17 août 2010) | 3 lines
Modernize gzip examples
........
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/library/gzip.rst | 20 |
1 files changed, 8 insertions, 12 deletions
diff --git a/Doc/library/gzip.rst b/Doc/library/gzip.rst index 1f64428..022bef7 100644 --- a/Doc/library/gzip.rst +++ b/Doc/library/gzip.rst @@ -88,26 +88,22 @@ Examples of usage Example of how to read a compressed file:: import gzip - f = gzip.open('/home/joe/file.txt.gz', 'rb') - file_content = f.read() - f.close() + with gzip.open('/home/joe/file.txt.gz', 'rb') as f: + file_content = f.read() Example of how to create a compressed GZIP file:: import gzip - content = "Lots of content here" - f = gzip.open('/home/joe/file.txt.gz', 'wb') - f.write(content) - f.close() + content = b"Lots of content here" + with gzip.open('/home/joe/file.txt.gz', 'wb') as f: + f.write(content) Example of how to GZIP compress an existing file:: import gzip - f_in = open('/home/joe/file.txt', 'rb') - f_out = gzip.open('/home/joe/file.txt.gz', 'wb') - f_out.writelines(f_in) - f_out.close() - f_in.close() + with open('/home/joe/file.txt', 'rb') as f_in: + with f_out = gzip.open('/home/joe/file.txt.gz', 'wb') as f_out: + f_out.writelines(f_in) .. seealso:: |