diff -r 9f4b066754c3 Doc/library/gzip.rst --- a/Doc/library/gzip.rst Tue Jun 23 14:31:11 2015 +0200 +++ b/Doc/library/gzip.rst Tue Jun 23 16:35:08 2015 +0200 @@ -96,26 +96,21 @@ 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() + with open('file.txt', 'rb') as f_in, gzip.open('file.txt.gz', 'wb') as f_out: + f_out.writelines(f_in) .. seealso::