python中处理文件中邮箱名分类

#邮箱分类
import os
import collections

def work(path):
resPath=r"D:\f\Python\pycharm\234\分类"

#打开文件
with open(path,"r")as f:
while True:
lineInfo=f.readline()#处理这一行文件sdfakj5564@163.com---dfa
if len(lineInfo)<5:
break
#邮箱字符串sdfakj5564@163.com
mailStr=lineInfo.split("---")[0]#用---切割字符串,取第一个
#得到x为163
fileType= mailStr.split("@")[1].split(".")[0]
#得到一个以x命名的绝对路径 D:\学习\Python\pycharm\234\分类\163
dirStr = os.path.join(resPath,fileType)
if not os.path.exists(dirStr):#判断x的目录是否存在,不存在则创建一个
os.mkdir(dirStr)

filePath= os.path.join(dirStr,fileType+".tex")
with open(filePath,"a")as fw:
fw.write(mailStr+"\n")#换行写入文件


def getAllDirQU(path):
queue=collections.deque()
#进队
queue.append(path)

while len(queue)!=0:
#出队数据
dirPath=queue.popleft()
#找出dirPath所有的文件
filesList=os.listdir(dirPath)

for fileName in filesList:
#绝对路径
fileAbsPath=os.path.join(dirPath,fileName)
#判断是否是目录
if os.path.isdir(fileAbsPath):
queue.append(fileAbsPath)
else:
#处理普通文件
work(fileAbsPath)

getAllDirQU(r"D:\f\Python\pycharm\234\分类")
posted @ 2019-03-03 15:59  飞飞阿  阅读(526)  评论(0编辑  收藏  举报