diff --git a/cookbook/c04/p16_iterate_while.py b/cookbook/c04/p16_iterate_while.py index bf140ee..c0300fc 100644 --- a/cookbook/c04/p16_iterate_while.py +++ b/cookbook/c04/p16_iterate_while.py @@ -22,11 +22,10 @@ def reader2(s, size): def iterate_while(): CHUNKSIZE = 8192 - f = open('/etc/passwd') - for chunk in iter(lambda: f.read(10), ''): - n = sys.stdout.write(chunk) + with open('/etc/passwd') as f: + for chunk in iter(lambda: f.read(10), ''): + n = sys.stdout.write(chunk) if __name__ == '__main__': iterate_while() -