首页 > 解决方案 > 在python3.x中将大文件拆分为多个文件

问题描述

如果 file_write 的文件大小大于 20MB,我想将文件拆分为多个文件。在 Random 函数中,我打开 big_file.txt 并使用 remove_noise() 去除噪声并将干净的行写入 outfile。我不确定如何根据当前实现中的大小拆分文件。请找到下面的代码:(抱歉没有提供正确的实现示例,因为它真的很复杂)我已经通过这个链接的例子:Split large text file(about 50GB) into multiple files

import os
def parses(lines, my_date_list):
    for line in reversed(list(lines)):
        line = line.strip()
        if not line:
            continue

        date_string = "2019-11-01" # assumption
        yield date_string, line


def remove_noise(line):
    """ dummy function"""
    return line



def random_function(path, output, cutoff="2019-10-31"):

    my_date_list = []
    if os.path.exists(path):
        with open(path) as f:
            lines = parses(f, my_date_list)
            for date, line in lines:
                if cutoff <= date:
                    results = remove_noise(line)
                    output.write(results + '\n')
                    continue
                else:
                    break

在向 写入行时output,我需要检查大小。如果大小达到 20MB,我想把它写到第二个 {may be output_2} 等等。

if __name__ == '__main__':
    path = "./big_file.txt"
    file_write = "./write_file.txt"
    with open(file_write) as outfile:
        random_function(path=path, output=outfile)

标签: python-3.xfilesplit

解决方案


推荐阅读