summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2010-08-17 21:11:49 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2010-08-17 21:11:49 (GMT)
commitbf1a018ce4358af60f3dc41262b04d01091aeebf (patch)
tree7f9c44ccae70314e22b02cbc5acb577b10edf746
parent79c5ef11d56e761445e334a7121764808e9e70cf (diff)
downloadcpython-bf1a018ce4358af60f3dc41262b04d01091aeebf.zip
cpython-bf1a018ce4358af60f3dc41262b04d01091aeebf.tar.gz
cpython-bf1a018ce4358af60f3dc41262b04d01091aeebf.tar.bz2
Modernize gzip examples
-rw-r--r--Doc/library/gzip.rst20
1 files changed, 8 insertions, 12 deletions
diff --git a/Doc/library/gzip.rst b/Doc/library/gzip.rst
index 0401cd8..060f48e 100644
--- a/Doc/library/gzip.rst
+++ b/Doc/library/gzip.rst
@@ -102,26 +102,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)
Example of how to GZIP compress a binary string::