with语句

Posted 老王的农场

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了with语句相关的知识,希望对你有一定的参考价值。

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语句的主要内容,如果未能解决你的问题,请参考以下文章

sql里面 with...as 是啥意思啊?如何使用

如何包装所有片段(不在里面)or) with?

[React Testing] Use Generated Data in Tests with tests-data-bot to Improve Test Maintainability(代码片段

揭秘 Python 中的 with 关键字

add application window with unknown token XXX Unable to add window;is your activity is running?(代码片段

add application window with unknown token XXX Unable to add window;is your activity is running?(代码片段