理解Python的with as语句

简单的说,

with open(filepath, 'wb') as file:

  file.write("something")

等价于:

file = open(filepath)

try:

  file.write("something", 'wb')

finally:

  file.close()

总的来说with... as包含一个错误处理和一个文件关闭功能!

原文地址:https://www.cnblogs.com/yrqiang/p/5295371.html