首页 > 编程语言 >python文件切割

python文件切割

时间:2022-09-21 15:35:28浏览次数:67  
标签:__ 文件 切割 python datetime dataList target dir name

把多个文件按照行数分割成几个文件

from datetime import datetime


def Main():
    source_dir = 'result.txt'
    target_dir = './'

    # 计数器
    flag = 0

    # 文件名
    name = 1

    # 存放数据
    dataList = []

    print("开始。。。。。")
    print(datetime.now().strftime('%Y-%m-%d %H:%M:%S'))

    with open(source_dir, 'r') as f_source:
        for line in f_source:
            flag += 1
            dataList.append(line)
            if flag == 1000000:
                with open(target_dir + "jz_yuanshi_list_" + str(name) + ".txt", 'w+') as f_target:
                    for data in dataList:
                        f_target.write(data)
                name += 1
                flag = 0
                dataList = []

    # 处理最后一批行数少于200万行的
    with open(target_dir + "jz_yuanshi_list_" + str(name) + ".txt", 'w+') as f_target:
        for data in dataList:
            f_target.write(data)

    print("完成。。。。。")
    print(datetime.now().strftime('%Y-%m-%d %H:%M:%S'))


if __name__ == "__main__":
    Main()

标签:__,文件,切割,python,datetime,dataList,target,dir,name
From: https://www.cnblogs.com/pangya/p/16715748.html

相关文章