理解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包含一个错误处理和一个文件关闭功能!

posted @ 2016-03-19 15:43  高手教程  阅读(494)  评论(0编辑  收藏  举报