Python—读取文件夹中的所有文件,并为每个文件创建唯一的输出

Python—读取文件夹中的所有文件,并为每个文件创建唯一的输出,python,directory,Python,Directory,我正试图将两个巨大的源文件(每行都有相应的内容)拆分为几个较小的文件,每个文件都包含唯一的输入,但到目前为止,一切都没有进展。我想让一个方法读取输出目录中的所有文件,并将它们的内容加入到某个黑名单中。因此,首先,这个黑名单是空的,因为文件是空的,我想读取源文件并将n行复制到第一个较小的文件中,并将内容添加到黑名单中。接下来,我将检查列表并将行写入第二个文件n次,前提是它们不在所述黑名单中。出于某种原因,在我附加了第一次读取的内容之后,我没有得到任何黑名单的输入。 以下是我得到的: def che

我正试图将两个巨大的源文件(每行都有相应的内容)拆分为几个较小的文件,每个文件都包含唯一的输入,但到目前为止,一切都没有进展。我想让一个方法读取输出目录中的所有文件,并将它们的内容加入到某个黑名单中。因此,首先,这个黑名单是空的,因为文件是空的,我想读取源文件并将
n
行复制到第一个较小的文件中,并将内容添加到黑名单中。接下来,我将检查列表并将行写入第二个文件
n
次,前提是它们不在所述黑名单中。出于某种原因,在我附加了第一次读取的内容之后,我没有得到任何黑名单的输入。 以下是我得到的:

def check_overlap(path):
# check if lines appear in other files

    content = []
    for filename in os.listdir(path):
        with open(path + filename, "r", encoding="utf-8") as f:
            content.append(f.read())
            print(filename + str(content))
            # when I print this out, it's empty for the first file
            # the other 3 files have the desired output, but why?
            # How is it empty after I appended the content of f?
            f.close()

    all_content = "".join(content)
    return all_content


def shuffle_data(n, source, output):
# shuffle source into n portions while keeping each line unique

    with open(output, "w", encoding="utf-8") as shuffled_file:

        existing_files = check_overlap()

        with open(source, 'r', encoding="utf-8") as source:
            i = 0
            for line in source:
                if i < n and line not in existing_files:
                    shuffled_file.write(line)
                    i += 1

shuffle_data(50, "source1", "output_50A")
shuffle_data(50, "source2", "output_50B")
shuffle_data(200, "source1", "output_200A")
shuffle_data(200, "source2", "output_200B")
它们必须保留相应的行,但由于我得到的错误:

Output 1    Output 2
dog         dogs
book        books
horse       flowers
flowers     eggs

因此,它似乎跳过了随机行,因为它的黑名单不稳定。每次我运行程序时,源都是随机的,所以它们在哪一行开始发散总是不同的。所有输出文件都在同一个目录中,源文件在不同的目录中。

根据注释,尝试类似的操作。如果源文件在大小之前用完,则可能需要处理异常

Sizes = [50, 100, 200, 600, 1000, 3000, 10000]


with open('file1') as f1:
    with open('file2') as f2:
        sources = iter(zip(f1, f2))

        for size in Sizes:
            o1_name = 'output_{}A'.format(size)
            o2_name = 'output_{}B'.format(size)
            with open(o1_name, 'w') as o1:
                with open(o2_name, 'w') as o2:
                    for _ in range(size):
                        l1,l2 = next(sources)
                        o1.write(l1.strip())
                        o2.write(l2.strip())

根据评论,试试这样的方法。如果源文件在大小之前用完,则可能需要处理异常

Sizes = [50, 100, 200, 600, 1000, 3000, 10000]


with open('file1') as f1:
    with open('file2') as f2:
        sources = iter(zip(f1, f2))

        for size in Sizes:
            o1_name = 'output_{}A'.format(size)
            o2_name = 'output_{}B'.format(size)
            with open(o1_name, 'w') as o1:
                with open(o2_name, 'w') as o2:
                    for _ in range(size):
                        l1,l2 = next(sources)
                        o1.write(l1.strip())
                        o2.write(l2.strip())

你能从细节中退一步,解释一下你想要完成什么吗?看起来你想把你的输入文件分成一定长度的片段?如果是这样的话,有一个unix实用程序,我有两个文件,我想把每个文件分成50行,一个较小的文件,200行,另一个较小的文件。但是对于200行,我不希望从50行文件中重复任何行,因此我尝试创建一个黑名单,只放入以前的文件中不存在的行。每个较小文件的行数不同。输入文件有多长?250行,或者其他什么?大约90.000行,需要生成18个文件,而meThat的平均每个文件5000行。你能从细节中退一步,解释一下你想要完成什么吗?看起来你想把你的输入文件分成一定长度的片段?如果是这样的话,有一个unix实用程序,我有两个文件,我想把每个文件分成50行,一个较小的文件,200行,另一个较小的文件。但是对于200行,我不希望从50行文件中重复任何行,因此我尝试创建一个黑名单,只放入以前的文件中不存在的行。每个较小文件的行数不同。输入文件有多长?250行,或者其他什么?大约90.000行,需要生成18个文件,而meThat的平均每个文件5000行。它将如何工作?