[Python-checkins] r84158 - in python/branches/release31-maint: Doc/library/gzip.rst
antoine.pitrou
python-checkins at python.org
Tue Aug 17 23:15:19 CEST 2010
Author: antoine.pitrou
Date: Tue Aug 17 23:15:19 2010
New Revision: 84158
Log:
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
........
Modified:
python/branches/release31-maint/ (props changed)
python/branches/release31-maint/Doc/library/gzip.rst
Modified: python/branches/release31-maint/Doc/library/gzip.rst
==============================================================================
--- python/branches/release31-maint/Doc/library/gzip.rst (original)
+++ python/branches/release31-maint/Doc/library/gzip.rst Tue Aug 17 23:15:19 2010
@@ -88,26 +88,22 @@
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::
More information about the Python-checkins
mailing list