# coding:utf-8
#首先创建有ABC三个段落的字符串------------------------------------------------
zimu=["A",'B','C']
content=""
for alpha in zimu:
for i in range(6):
if i%2==0:
sb="a"
else:
sb="b"
con=sb+":nihao,"+str(i+1)+alpha+"\n"
content+=con
if alpha=="C":
break
else:
content+="====================================\n"
print(content)
#将字符串写入一个文本文件---------------------------------------------------------------
with open(r"C:\Users\arorn\Desktop\pythonfileNewWritIn.txt","w+") as f:
f.write(content)
import os
#os.rmdir(r"C:\Users\arorn\Desktop\testFile")-------------只能删除空的目录
#os.remove(r"C:\Users\arorn\Desktop\testFile\1.txt")--------------删除指定文件,括号里写绝对路径
import shutil
shutil.rmtree(r"C:\Users\arorn\Desktop\testFile")#此种方法可以删除非空的目录
os.makedirs(r"C:\Users\arorn\Desktop\testFile")
#读取文本
with open(r"C:\Users\arorn\Desktop\pythonfileNewWritIn.txt","r") as f:
text=f.read()
print(text)#这里也可以使用open的方法打开文件,下面处理的时候用for i in f一行行处理
asays=""
bsays=""
#定义分割的方法,将每一段的对话分流成a,b两个字符串
def says(role,said):
global asays,bsays
if role=="a":
asays=asays+said+"\n"
return asays
if role=="b":
bsays=bsays+said+"\n"
return bsays
#每一段结束,就创建目录,将文本写入对应文件
def makeSaysDir(a,b,duanluo):
patha=r"C:\Users\arorn\Desktop\testFile\duanluo"+str(duanluo)+"-A-says.txt"
pathb=r"C:\Users\arorn\Desktop\testFile\duanluo"+str(duanluo)+"-B-says.txt"
with open(patha,"w+") as f:
f.write(a)
with open(pathb,"w+") as f:
f.write(b)
duanluo=1
list=text.split("\n")
#主循环--------------------------------------------------------------------------------------------------
for eachline in list:
#print(eachline)
if eachline[:5]!="=====" and len(eachline)!=0:#判断当前行为有文字内容的文本行
#print(eachline)
(role,said)=eachline.split(":")
says(role,said)
else:
print(asays,bsays)
makeSaysDir(asays,bsays,duanluo)
duanluo+=1
asays=""
bsays=""