我想将许多文件视为一个文件。将 [filenames] => [file objects] => [lines] 与生成器/不将整个文件读入内存的正确 pythonic 方式是什么?
我们都知道打开文件的正确方法:
with open("auth.log", "rb") as f:
print sum(f.readlines())
我们知道将多个迭代器/生成器链接成一个长的一个的正确方法:
>>> list(itertools.chain(range(3), range(3)))
[0, 1, 2, 0, 1, 2]
但是如何将多个文件链接在一起并保留上下文管理器?
with open("auth.log", "rb") as f0:
with open("auth.log.1", "rb") as f1:
for line in itertools.chain(f0, f1):
do_stuff_with(line)
# f1 is now closed
# f0 is now closed
# gross
我可以忽略上下文管理器并做这样的事情,但感觉不对:
files = itertools.chain(*(open(f, "rb") for f in file_names))
for line in files:
do_stuff_with(line)
或者这就是Async IO - PEP 3156的用途,我只需要稍后等待优雅的语法吗?