Skip to content

Commit 43aa61e

Browse files
authored
Merge pull request yidao620c#194 from cclauss/patch-3
Use "with open()" to ensure the file gets close()d
2 parents 8b0e903 + 49d2702 commit 43aa61e

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

cookbook/c04/p16_iterate_while.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
import sys
88

99

10+
def process_data():
11+
print(data)
12+
13+
1014
def reader(s, size):
1115
while True:
1216
data = s.recv(size)
@@ -22,11 +26,10 @@ def reader2(s, size):
2226

2327
def iterate_while():
2428
CHUNKSIZE = 8192
25-
f = open('/etc/passwd')
26-
for chunk in iter(lambda: f.read(10), ''):
27-
n = sys.stdout.write(chunk)
29+
with open('/etc/passwd') as f:
30+
for chunk in iter(lambda: f.read(10), ''):
31+
n = sys.stdout.write(chunk)
2832

2933

3034
if __name__ == '__main__':
3135
iterate_while()
32-

0 commit comments

Comments
 (0)