python文件写入写出操作之文件分割

f=open('E:\\python\\python程序\\a.txt')
one=[]
two=[]
count=0

for i in f:
        if i[:3]!='===':
                (r,n)=i.split(':',1)
                if r=='1':
                        one.append(n)
                else:
                        two.append(n)
        else:
                count+=1
                f1=open('E:\\python\\python程序\\b'+str(count)+'.txt','w')
                f2=open('E:\\python\\python程序\\c'+str(count)+'.txt','w')
                f1.writelines(one)
                f2.writelines(two)
                f1.close()
                f2.close()
                one=[]
                two=[]
f.close()
在这里插入代码片
发布了130 篇原创文章 · 获赞 16 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/feiqipengcheng/article/details/103754182