关于python:python-txt文件分割

7次阅读

共计 506 个字符,预计需要花费 2 分钟才能阅读完成。

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')
正文完
 0