码迷,mamicode.com
首页 > 其他好文 > 详细

with语句

时间:2017-12-23 14:07:47      阅读:91      评论:0      收藏:0      [点我收藏+]

标签:语句   上下文   dex   2.7   word   post   log   pac   open   

with语句

为了避免打开文件后忘记关闭,可以通过管理上下文,即:

1
2
3
with open(‘log‘,‘r‘) as f:
     
    ...

如此方式,当with代码块执行完毕时,内部会自动关闭并释放文件资源。

在Python 2.7 后,with又支持同时对多个文件的上下文进行管理,即:

1
2
with open(‘log1‘) as obj1, open(‘log2‘) as obj2:
    pass

 

with语句

标签:语句   上下文   dex   2.7   word   post   log   pac   open   

原文地址:http://www.cnblogs.com/catherine007/p/8093132.html

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