文件处理

import os

def fun1(path1):
stack = []
stack.append(path1)
# print(stack)
while len(stack) != 0 :
path1 = stack.pop()
dir_path = os.listdir(path1)
for i in dir_path:
if os.path.isdir(os.path.join(path1,i)):
stack.append(os.path.join(path1,i))
else:
# print(os.path.join(path1,i))
fun2(os.path.join(path1,i),path1)
def fun2(path1,path_dir):
with open(path1,"r") as f :
while True:
line_info = f.readline()
if len(line_info) < 5:
break
# print(line_info)
#邮箱的类型
line_em = (line_info.split("@")[1]).split(".")[0]
#获取目录
path2 = os.path.dirname(path_dir)
# print(line_em)
#邮箱存放的目录
dir_path = os.path.join(path2,line_em)
if not os.path.exists(dir_path):
os.mkdir(dir_path)
file_path = os.path.join(dir_path,line_em + ".txt")
with open(file_path,"a") as f10:
f10.write(line_info)
fun1(r"D:\python_code\haha")

转载于:https://www.cnblogs.com/chen-wg/p/10784052.html

你可能感兴趣的:(文件处理)