1 import os
2 import collections
3
4 def work(path):
5 resPath = r"G:\python学习课件\python学习笔记\千锋教育\day\day10\res"
6 # 打开文件
7 with open(path, "r") as f:
8 while True:
9 # laphae11985@163.com----198587
10 lineInfo = f.readline()
11 if len(lineInfo) < 5:
12 break
13 # 邮箱的字符串
14 # laphae11985@163.com
15 emailStr = lineInfo.split("----")[0]
16 # 邮箱类型的目录
17 # G:\python学习课件\python学习笔记\千锋教育\day\day10\res\163
18 fileType = emailStr.split("@")[1].split(".")[0]
19 dirStr = os.path.join(resPath, fileType)
20 if not os.path.exists(dirStr):
21 # 不存在,创建
22 os.mkdir(dirStr)
23 filePath = os.path.join(dirStr, fileType + ".txt")
24 with open(filePath, "w") as fw:
25 fw.write(emailStr + "\n")
26
27 def getAllDirQU(path):
28 queue = collections.deque()
29 queue.append(path)
30 while len(queue) != 0:
31 dirPath = queue.popleft()
32 fileList = os.listdir(dirPath)
33 for fileName in fileList:
34 fileAbsPath = os.path.join(dirPath, fileName)
35 if os.path.isdir(fileAbsPath):
36 queue.append(fileAbsPath)
37 else:
38 # 处理普通文件
39 work(fileAbsPath)
40
41 getAllDirQU(r"G:\python学习课件\python学习笔记\千锋教育\day\day10\data")