diff options
author | Berker Peksag <berker.peksag@gmail.com> | 2015-06-25 20:57:42 (GMT) |
---|---|---|
committer | Berker Peksag <berker.peksag@gmail.com> | 2015-06-25 20:57:42 (GMT) |
commit | 1ecb5ce70762b979111ed4f0388d419f76c70867 (patch) | |
tree | ceb423c622ed8474be4408ffafbdea149a07effe | |
parent | 6750c8b803f0f6d257a6bac36d695bc5fb049bf9 (diff) | |
download | cpython-1ecb5ce70762b979111ed4f0388d419f76c70867.zip cpython-1ecb5ce70762b979111ed4f0388d419f76c70867.tar.gz cpython-1ecb5ce70762b979111ed4f0388d419f76c70867.tar.bz2 |
Issue #24496: Backport gzip examples to Python 2.
gzip.open() supports context management protocol in Python 2, so it's better to
use it in the examples section.
Patch by Jakub Kadlčík.
-rw-r--r-- | Doc/library/gzip.rst | 18 |
1 files changed, 7 insertions, 11 deletions
diff --git a/Doc/library/gzip.rst b/Doc/library/gzip.rst index e26fe28..7c16d3a 100644 --- a/Doc/library/gzip.rst +++ b/Doc/library/gzip.rst @@ -96,26 +96,22 @@ Examples of usage Example of how to read a compressed file:: import gzip - f = gzip.open('file.txt.gz', 'rb') - file_content = f.read() - f.close() + with gzip.open('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('file.txt.gz', 'wb') - f.write(content) - f.close() + with gzip.open('file.txt.gz', 'wb') as f: + f.write(content) Example of how to GZIP compress an existing file:: import gzip - f_in = open('file.txt', 'rb') - f_out = gzip.open('file.txt.gz', 'wb') - f_out.writelines(f_in) - f_out.close() - f_in.close() + import shutil + with open('file.txt', 'rb') as f_in, gzip.open('file.txt.gz', 'wb') as f_out: + shutil.copyfileobj(f_in, f_out) .. seealso:: |