with open(source_dir, 'r' ,encoding='utf-8') as f_source: for line in f_source: flag += 1 dataList.append(line) if flag == chaifen_line: filePath = os.path.join(target_dir, "new_chaifei_" + str(name) + ".txt") with open( filePath, 'w+', encoding='utf-8') as f_target: for data in dataList: f_target.write(data) name += 1 flag = 0 dataList = []
# 处理最后一批行数少于4.5万行的 filePath = os.path.join(target_dir, "new_chaifei_" + str(name) + ".txt") with open(filePath, 'w+') as f_target: for data in dataList: f_target.write(data)