将文件分割成块:我的代码有什么问题?

2024-09-29 23:23:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我的方法是这样的:

def fChunks(l, n):
    """Chunks iterable into n sized chunks"""
    for i in range(0, len(l), n):
        yield l[i:i + n]
        
folder=r"_my_folder"
berkas_ori = pat(fr"{folder}\my_text.txt")

try:
    lines = []
    with open(berkas_ori,'r') as main_file:
#         head = [next(main_file) for x in range(1000)]
        total_baris = sum(1 for line in main_file)
        print(f"Total jlh baris: {total_baris:,.0f}")
        for line in main_file:
            lines.append(line)
        jlh_baris_chunk = math.ceil(total_baris/5)
        print(f"Jlh baris per chunk= {jlh_baris_chunk}")
        
    # Write each group of lines to separate files
    for i, group in enumerate(fChunks(lines, n=jlh_baris_chunk), start=1):
        berkas_tgt=pat(fr"{folder}\chunks\my_text_{i}.txt")
        with open(berkas_tgt, mode="w") as out_file:
            for line in group:
                out_file.write(line)
            print(f"Finish generating {berkas_tgt}")
except FileNotFoundError as e:
    print(e)

预计3137672行的文件将被平均分割为5个文件块。 没有错误,但不知怎么的,它并没有像我预期的那样成功。没有创建任何文件

我在这里忽略了什么


Tags: informainmyaslinefolderfile
2条回答

在文件行上进行第一次迭代后,需要将文件光标返回到零

main_file.seek(0)

之后

total_baris = sum(1 for line in main_file)

我应该做这项工作

进行一些试验后,添加: with open(berkas_ori,'r') as main_file:之前再次 for line in main_file:

将是:

def fChunks(l, n):
    """Chunks iterable into n sized chunks"""
    for i in range(0, len(l), n):
        yield l[i:i + n]
        
folder=r"_my_folder"
berkas_ori = pat(fr"{folder}\my_text.txt")

try:
    lines = []
    with open(berkas_ori,'r') as main_file:
#         head = [next(main_file) for x in range(1000)]
        total_baris = sum(1 for line in main_file)
        print(f"Total jlh baris: {total_baris:,.0f}")
    with open(berkas_ori,'r') as main_file:
        for line in main_file:
            lines.append(line)
        jlh_baris_chunk = math.ceil(total_baris/5)
        print(f"Jlh baris per chunk= {jlh_baris_chunk}")
        
    # Write each group of lines to separate files
    for i, group in enumerate(fChunks(lines, n=jlh_baris_chunk), start=1):
        berkas_tgt=pat(fr"{folder}\chunks\my_text_{i}.txt")
        with open(berkas_tgt, mode="w") as out_file:
            for line in group:
                out_file.write(line)
            print(f"Finish generating {berkas_tgt}")
except FileNotFoundError as e:
    print(e)

它解决了问题,并按预期创建了5个文件

很抱歉给您带来不便

相关问题 更多 >

    热门问题