From 8c4fd325a98bdc2671100cd21e63d62c30860133 Mon Sep 17 00:00:00 2001 From: Mark Pilgrim Date: Mon, 17 Aug 2009 15:19:11 -0400 Subject: [PATCH] clarified with statement origin --- files.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/files.html b/files.html index 82134ae..76b4342 100644 --- a/files.html +++ b/files.html @@ -206,7 +206,7 @@ ValueError: I/O operation on closed file.

Stream objects have an explicit close() method, but what happens if your code has a bug and crashes before you call close()? That file could theoretically stay open for much longer than necessary. While you’re debugging on your local computer, that’s not a big deal. On a production server, maybe it is. -

Python 2 had a solution for this: the try..finally block. That still works in Python 3, and you may see it in other people’s code or in older code that was ported to Python 3. But Python 3 also adds a cleaner solution: the with statement. +

Python 2 had a solution for this: the try..finally block. That still works in Python 3, and you may see it in other people’s code or in older code that was ported to Python 3. But Python 2.5 introduced a cleaner solution, which is now the preferred solution in Python 3: the with statement.

with open('examples/chinese.txt', encoding='utf-8') as a_file:
     a_file.seek(17)