from __future__ import with_statement
with open("hello.txt") as f:
for line in f:
print line
而不是:
f = open("hello.txt")
try:
for line in f:
print line
finally:
f.close()
【在 n*e 的大作中提到】 : from __future__ import with_statement : with open("hello.txt") as f: : for line in f: : print line : 而不是: : f = open("hello.txt") : try: : for line in f: : print line : finally: