相关疑难解决方法(0)

迭代多个文件的所有行的最pythonic方法是什么?

我想将许多文件视为一个文件.什么是正确的pythonic方式采用[filenames] => [文件对象] => [行]与生成器/不读取整个文件到内存?

我们都知道打开文件的正确方法:

with open("auth.log", "rb") as f:
    print sum(f.readlines())
Run Code Online (Sandbox Code Playgroud)

我们知道将几个迭代器/生成器链接到一个长迭代器的正确方法:

>>> list(itertools.chain(range(3), range(3)))
[0, 1, 2, 0, 1, 2]
Run Code Online (Sandbox Code Playgroud)

但是如何将多个文件链接在一起并保留上下文管理器?

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
Run Code Online (Sandbox Code Playgroud)

我可以忽略上下文管理器并执行类似的操作,但感觉不对:

files = itertools.chain(*(open(f, "rb") for f in file_names))
for line in files:
    do_stuff_with(line)
Run Code Online (Sandbox Code Playgroud)

或者这是Async IO-PEP 3156的用途,我只需要等待优雅的语法?

python logging parsing file

14
推荐指数
1
解决办法
5673
查看次数

标签 统计

file ×1

logging ×1

parsing ×1

python ×1