python 大文件切割成小文件(常用)

-- coding: utf8 --

import os import time

def mkSubFile(lines, head, srcName, sub): [des_filename, extname] = os.path.splitext(srcName) filename = des_filename + '_' + str(sub) + extname print('make file: %s' % filename) fout = open(filename, 'w') try: fout.writelines([head]) fout.writelines(lines) return sub + 1 finally: fout.close()

def splitByLineCount(filename, count): fin = open(filename, 'r') try: head = fin.readline() buf = [] sub = 1 for line in fin: buf.append(line) if len(buf) == count: sub = mkSubFile(buf, head, filename, sub) buf = [] if len(buf) != 0: sub = mkSubFile(buf, head, filename, sub) finally: fin.close()

if name == 'main': begin = time.time() splitByLineCount(r'D:/file/python/today/password.txt', 600000) end = time.time() print('time is %d seconds ' % (end - begin))

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券