# -*- coding: utf-8 -*-
# __author__:'Administrator'
# @Time : 2018/8/31 14:19
import os
dst = "D:\\test" # 生成文件目录
# 将一个txt文件的内容,按照第几章进行分割
def SplitFile(file_path1, dst):
with open(file_path1, 'rb') as f1:
# 获取文件每一行
lines1 = f1.readlines()
# 获取file的名称
file_dir1 = file_path1.replace("\\", '/').split("/")[-1].split(".")[0]
path1 = os.path.join(dst, file_dir1)
if not os.path.exists(path1):
os.makedirs(path1)
i = 1
for line in lines1:
try:
if ("第" in line and "章 " in line) or ("第" in line and "章..." in line) or ("第" in line and "章\r\n" in line):
name = line.strip().decode('utf8')
i += 1
else:
fp = open(file_name1, 'ab+')
fp.write(line)