我有一个20个文件名的列表,比如['file1.txt', 'file2.txt',…]。我想写一个Python脚本将这些文件连接到一个新文件中。我可以通过f = open(…)打开每个文件,通过调用f.r edline()逐行读取,并将每行写入新文件。这对我来说似乎不是很“优雅”,尤其是我必须一行一行地读/写的部分。

在Python中是否有更“优雅”的方式来做到这一点?


当前回答

@inspectorG4dget答案的替代答案(到2016年3月29日为止的最佳答案)。我测试了3个436MB的文件。

@inspectorG4dget答案:162秒

解决方案:125秒

from subprocess import Popen
filenames = ['file1.txt', 'file2.txt', 'file3.txt']
fbatch = open('batch.bat','w')
str ="type "
for f in filenames:
    str+= f + " "
fbatch.write(str + " > file4results.txt")
fbatch.close()
p = Popen("batch.bat", cwd=r"Drive:\Path\to\folder")
stdout, stderr = p.communicate()

其思想是创建一个批处理文件并执行它,利用“旧的好技术”。它是半python,但运行速度更快。适用于windows。

其他回答

我不知道什么叫优雅,但这招管用:

    import glob
    import os
    for f in glob.glob("file*.txt"):
         os.system("cat "+f+" >> OutFile.txt")

UNIX命令有什么问题?(假设你不是在Windows上工作):

Ls | xargs cat | tee output.txt完成这项工作(如果你想要,你可以从python用subprocess调用它)

这样就行了

对于大文件:

filenames = ['file1.txt', 'file2.txt', ...]
with open('path/to/output/file', 'w') as outfile:
    for fname in filenames:
        with open(fname) as infile:
            for line in infile:
                outfile.write(line)

对于小文件:

filenames = ['file1.txt', 'file2.txt', ...]
with open('path/to/output/file', 'w') as outfile:
    for fname in filenames:
        with open(fname) as infile:
            outfile.write(infile.read())

我还想到了另一个有趣的问题:

filenames = ['file1.txt', 'file2.txt', ...]
with open('path/to/output/file', 'w') as outfile:
    for line in itertools.chain.from_iterable(itertools.imap(open, filnames)):
        outfile.write(line)

遗憾的是,最后一个方法留下了一些打开的文件描述符,无论如何GC都应该处理这些描述符。我只是觉得很有趣

使用shutil.copyfileobj。

它会自动读取输入文件的块为您,这是更有效的读取输入文件,即使一些输入文件太大,无法装入内存也能工作:

import shutil

with open('output_file.txt','wb') as wfd:
    for f in ['seg1.txt','seg2.txt','seg3.txt']:
        with open(f,'rb') as fd:
            shutil.copyfileobj(fd, wfd)
def concatFiles():
    path = 'input/'
    files = os.listdir(path)
    for idx, infile in enumerate(files):
        print ("File #" + str(idx) + "  " + infile)
    concat = ''.join([open(path + f).read() for f in files])
    with open("output_concatFile.txt", "w") as fo:
        fo.write(path + concat)

if __name__ == "__main__":
    concatFiles()