Log拆分工具——Text cutting

说明:适用于拆分大Log,默认固定1000000行拆分一个文件

           start.bat、Textcutting.py两个脚本请放置在同级目录下


启动:start.bat

源码:

@echo off

echo /*************************************************************************

echo *******************************Text cutting*******************************

echo *************************************************************************/

python Textcutting.py

pause


主程序:Textcutting.py

源码:

# -*- coding: cp936 -*-

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):

    global count

    count = 1000000

    print('This program will cut your file in %s lines') % count

    choose = raw_input('Do you want to change lines count?')

    if choose == 'Y' or choose == 'y' or choose == 'yes':

        new_count = raw_input('Please input your count:')

        count = new_count

        print('OK!This program now will cut your file in %s lines') % count

    fin = open(filename, 'r')

    length = len(["" for line in open(filename, "r")])

    if length <= int(count):

        print 'The total number of lines in the current file are %s.' % length

        print 'Current file lines are smaller than your count, do not need to split.'

        select = raw_input('Continue or not:')

        if select == 'Y' or select == 'y' or select == 'yes':

            splitByLineCount(choose_file())

        return

    try:

        head = fin.readline()

        buf = []

        sub = 1

        for line in fin:

            buf.append(line)

            if len(buf) == int(count):

                sub = mkSubFile(buf, head, filename, sub)

                buf = []

        if len(buf) != 0:

            sub = mkSubFile(buf, head, filename, sub)

    finally:

        again = raw_input('Do you want to split another file?')

        if again == 'Y' or again == 'y' or again == 'yes':

            splitByLineCount(choose_file())

        fin.close()

def choose_file():

    global filename

    filepath = raw_input('Please choose a file:')

    checkout = os.path.exists(filepath)

    if not checkout:

        print('No such file.')

        choose_file()

    filename = filepath

    return filename

if __name__ == '__main__':

    begin = time.time()

    splitByLineCount(choose_file())

    end = time.time()

    print('time is %d seconds ' % (end - begin))

你可能感兴趣的:(Log拆分工具——Text cutting)