在单个表达式中读取文件,正确关闭它们
Read files in a single expression, properly closing them
我想读取 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())
...非常冗长,读起来很长。
还有其他选择吗?
你可以使用一个函数:
def read_contents(filename):
with open(filename) as file:
return file.read()
contents = [read_contents(f) for f in files]
您可以使用 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]
我想读取 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())
...非常冗长,读起来很长。
还有其他选择吗?
你可以使用一个函数:
def read_contents(filename):
with open(filename) as file:
return file.read()
contents = [read_contents(f) for f in files]
您可以使用 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]