txt大文件拆分

这里介绍两种方法:1、批处理;2、python
参考文章:1、批处理;2、python
总结:python批处理速度快很多,源代码如下

1、批处理:

@echo off & setlocal enabledelayedexpansion
set /a m=0,n=0

for /f "delims=" %%a in (test.txt) do (
        set /a n=m/1048576
        set /a m=m+1
        echo %%a  >>test!n!.txt
)

2、python:

def split():
    # 读取源文件,文件名最好加上绝对路径
    with open('test.txt', 'r') as f:
        # 把数据写入列表
        wordlist = f.readlines()
        # 算出总行数
        length = len(wordlist)
    # 设置每个拆分文件的行数
    unit = 1048576
    # 计算新文件的个数,如果总行数整除新文件行数,就取这个商的值,如果不整除,取商加1的值
    file_amount = length // unit + 1 if length % unit > 0 else length // unit
    # 遍历所有新文件
    for num in range(file_amount):
        # 计算新文件中第一行在源文件中对应的行号
        start = num * unit
        # 计算新文件中最后一行在源文件中对应的行号
        end = length if length < (num + 1) * unit else (num + 1) * unit
        # 写入新文件,文件名最好加上绝对路径
        with open(str(num + 1) + '.txt', 'w+') as f:
            # 遍历新文件的所有行
            for i in range(start, end):
                # 把列表中的数据写入新文件
                f.write(wordlist[i])
 
if __name__ == '__main__':
    split()
  • 10
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值