]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Modernize gzip examples
authorAntoine Pitrou <solipsis@pitrou.net>
Tue, 17 Aug 2010 21:11:49 +0000 (21:11 +0000)
committerAntoine Pitrou <solipsis@pitrou.net>
Tue, 17 Aug 2010 21:11:49 +0000 (21:11 +0000)
Doc/library/gzip.rst

index 0401cd8211608dde8d754531a219f27190d0c033..060f48e797a08d44e57d6c6d0246fb523f0d5054 100644 (file)
@@ -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::