码迷,mamicode.com
首页 > 编程语言 > 详细

理解Python的With as语句

时间:2016-03-19 16:04:08      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:

简单的说,

 

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

  file.write("something")

 

等价于:

 

file = open(filepath)

try:

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

finally:

  file.close()

 

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

理解Python的With as语句

标签:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!