读取单个表达式中的文件,并正确关闭它们



我想读取python中文件列表的内容。我的第一个想法是

contents = [open(f).read() for f in files]

但这会使文件处于打开状态,直到对象被垃圾收集,并显示ResourceWarning

关闭文件需要多种理解:

fds = [open(f) for f in files]
contents = [fd.read() for fd in fds]
[fd.close() for fd in fds]

这是不自然的。

或循环:

contents = []
for f in files:
with open(f) as fd:
contents.append(f.read())

这本书很冗长,读起来很长。

还有其他选择吗?

您可以为此使用pathlib。

from pathlib import Path
contents_text = [Path(f).read_text() for f in files]
contents_bytes = [Path(f).read_bytes() for f in files]

里面只是:

class Path:
# ....
def read_bytes(self):
"""
Open the file in bytes mode, read it, and close the file.
"""
with self.open(mode='rb') as f:
return f.read()
def read_text(self, encoding=None, errors=None):
"""
Open the file in text mode, read it, and close the file.
"""
with self.open(mode='r', encoding=encoding, errors=errors) as f:
return f.read()

您可以使用ExitStack上下文管理器。您的用例与文档中显示的示例略有不同。

from contextlib import ExitStack
with ExitStack() as es:
contents = [es.enter_context(open(f)).read() for f in files]

您可以使用一个函数:

def read_contents(filename):
with open(filename) as file:
return file.read()
contents = [read_contents(f) for f in files]

最新更新