import os
import shutil
import sys

def split_txt_into_folders(input_path):
    # 创建一个文件夹来保存拆分后的txt文件
    output_folder = os.path.splitext(input_path)[0]  # 获取输入文件的名称(不含扩展名)
    os.makedirs(output_folder, exist_ok=True)  # 如果文件夹已存在则不会报错

    # 读取输入的txt文件
    with open(input_path, 'r', encoding='utf-8') as input_file:
        # 初始化变量
        total_size = 0
        file_index = 1
        current_file = None
        
        # 逐行读取txt文件内容
        for line in input_file:
            # 检查是否需要新建文件
            if current_file is None or total_size + len(line.encode('utf-8')) > 5000:
                if current_file:
                    current_file.close()
                file_name = os.path.join(output_folder, f'part_{file_index}.txt')
                file_index += 1
                total_size = 0
                current_file = open(file_name, 'w', encoding='utf-8')
            
            # 写入行到当前文件
            current_file.write(line)
            total_size += len(line.encode('utf-8'))
            
        if current_file:
            current_file.close()

if __name__ == "__main__":
    args = sys.argv
    split_txt_into_folders(args[1])
    print("拆分完成!")

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐