遍历文件夹中的文件并创建一个新的合并文本文件

Loop through files in a folder and create a new merged text file

我正在努力将多个文本文件合并到一个文本文档中。我能够读取所有文件名并创建一个新的输出文档。

但是,当我输出文档时,我只从一个文件中获取数据而不是其他文件?总的来说一个txt应该接近100万行,但只得到前10k

import os

projpath1 = 'PATH1'
projpath2 = 'PATH2'

for root, dirs, files in os.walk(f"{projpath1}", topdown=False):
    for name in files:
        if not name.startswith('.DS_Store'):
            split = name.split("/")
            title = split[0]
            filename = (os.path.join(root, name))
            inputf = os.path.expanduser(f'{projpath1}/{title}')
            updatedf = os.path.expanduser(f'{projpath2}/ENC_merged.txt')

            with open(inputf, "r") as text_file, open(updatedf, 'w') as outfile:
                for info in text_file:
                        for lines in info:
                            outfile.write(lines)

我真的卡住了,想不通:/

用 bash

怎么样
ls | xargs cat > merged_file

您应该先打开创建输出文件,然后您需要在其中保存所有输入文件,这样的操作应该适合您。

import os

projpath1 = 'PATH1'
projpath2 = 'PATH2'
with open(updatedf, 'w') as outfile:
    for root, dirs, files in os.walk(f"{projpath1}", topdown=False):
        for name in files:
            if not name.startswith('.DS_Store'):
                split = name.split("/")
                title = split[0]
                filename = (os.path.join(root, name))
                inputf = os.path.expanduser(f'{projpath1}/{title}')
                updatedf = os.path.expanduser(f'{projpath2}/ENC_merged.txt')
                with open(inputf, "r") as text_file:
                    for info in text_file:
                        for lines in info:
                            outfile.write(lines)