python txt文件分割

dst = "log/all.txt"   # 生成文件目录

def splitFile(filePath):
    with open(filePath, 'r', encoding='utf-8') as f:
        line = f.readlines()
        aline = [i for i in line if i.strip('\n')]
        xx = 1
        fw = None
        for index, value in enumerate(aline):
            if value.startswith('------------'):
                if fw is not None:
                    fw.close()
                fw = open('log/' + str(xx) + '.log', 'w', encoding='utf-8')
                xx = xx + 1
            else:
                fw.write(value)


def read_log(filePath):
    with open(filePath, 'r', encoding='utf-8') as f:
        line = f.readlines()
        aline = [i for i in line if i.strip('\n')]
        for value in aline:
            if len(value) > 0 :
                print(value)


read_log('log/192.log')

你可能感兴趣的:(python)