Python引入了with语句来自动帮我们调用close()方法,同时也解决了异常问题
with open('test.txt','r') as f :
print(f.read())
等价于
try:
f =open('test1.txt','r')
print(f.read())
finally:
f.close()
Python引入了with语句来自动帮我们调用close()方法,同时也解决了异常问题
with open('test.txt','r') as f :
print(f.read())
等价于
try:
f =open('test1.txt','r')
print(f.read())
finally:
f.close()