版权声明:https://blog.csdn.net/qq_43546676 https://blog.csdn.net/qq_43546676/article/details/89084603
参考链接:http://www.cnblogs.com/ymjyqsx/p/6554817.html
with open('abc.txt','r') as f:
代码区
等价于:
try:
f = open('abc.txt','r')
代码区
except Exception as err:
print(err)
finally:
f.close()
这样不用每次手动关闭文件操作和异常处理,使得代码简洁,操作方便。