如何合并和拆分数千个文本文件?

How to merge and split again thousands of text files?

我有数千个 .txt 文件。这些文本文件包括一个字符串。 (每个文件都有不同的字符串。)

我想编辑这些字符串,但我不想一个一个地手动打开每个文件进行编辑。所以我想将所有这些文件合并到一个 .txt 文件中,在我完成编辑后,我想再次 seperate/split 使用它们在合并之前拥有的相同文件名。

例如;

我有这些文本文件。

lorem.txt(嗨,这是一个示例行。)

ipsum.txt(嗨,这是另一行。)

merol123.txt(嗨,只是另一行。)

*

merged.txt >>> edited and ready to split again. >> result needs to be like this;

*

lorem.txt(嗨,这是编辑过的行。)

ipsum.txt(另一行编辑。)

merol123.txt(另一行编辑。编号 4847887)

Note: Sentences inside brackets represents string inside txt file.

可能吗?我正在等待你的帮助,谢谢!

首先,我假设你没有正确地重复你的字符串(比如 "hi, this is an example line." != "hi, this is edited line."),而不是故意的(我可以'想不通)。

我将累积文件命名为 common.doc 以区别于目标目录中的其他 .txt 文件。此外,此示例代码暗示所有文件都在同一目录中。

# merging.py
import os
import glob

with open("common.doc", "w") as common:
    for txt in glob.glob("./*.txt"):
        with open(txt, "r") as f:
            content = f.read()
        common.write("{} ({})\n".format(os.path.basename(txt), content))

common.doc编辑后:

# splitting.py
with open("common.doc", "r") as common:
    for line in common:
        name = line[:line.find(" (")]
        text = line[line.find(" (")+2:line.rfind(")")]
        with open(name, "w") as f:
            f.write(text)

以及多行文本的解决方案(合并保留 .strip() 在内容编写中删除),不适合数十万个文件...

# splitting2.py
with open("common.doc", "r") as common:
    everything = common.read()
elements = everything.split(")")
for elem in elements:
    name = elem[:elem.find(" (")].strip()
    text = elem[elem.find(" (")+2:]
    if name:
        with open(name, "w") as f:
            f.write(text)