for open in paradigm python

时间:2014-11-10 08:13:19

标签: python file-io warnings

我总是用Python读取文件:

for line in open('filename'):
    pass

最近,python开始喷出这个警告:

ResourceWarning: unclosed file <_io.TextIOWrapper name='filename' mode='r' encoding='UTF-8'>

如果没有这种警告,什么是pythonic方式读取文件中的每一行?

with open('filename') as f:
    for line in f:
        pass

感觉像线条(和缩进级别)太多。

0 个答案:

没有答案