1、栈,先进后出
#模拟栈结构
stack = []
#圧栈(向栈里存数据)
stack.append("A")
print(stack)
stack.append("B")
print(stack)
stack.append("C")
print(stack)
#出栈
res1 = stack.pop()
print("res= " ,res1)
res2 = stack.pop()
print("res= " ,res2)
res3 = stack.pop()
print("res= " ,res3)
#2、队列,先进先出
import collections
queue = collections.deque()
print(queue)
#进队
queue.append("A")
print(queue)
queue.append("B")
print(queue)
queue.append("C")
print(queue)
#出队,取数据
res4 = queue.popleft()
print("res4= ", res4)
print(queue)
res5 = queue.popleft()
print("res4= ", res5)
print(queue)
res6 = queue.popleft()
print("res6= ", res6)
print(queue)
--------------------------
3、遍历目录
import os
def getAllDir(path, sp = ""):
#得到当前目录下所有的文件
fileList = os.listdir(path)
# print(fileList)
#处理每一个文件
sp += " "
for fileName in fileList:
#判断是否是路径(用绝对路径)
fileAbsPath = os.path.join(path, fileName)
if os.path.isdir(fileAbsPath):
print(sp + "目录:",fileName)
#递归调用
getAllDir(fileAbsPath, sp)
else:
print(sp + "普通文件:",fileName)
----------------------------------------------
4、栈模拟递归遍历目录
import os
def getAllDirDE(path):
stack = []
stack.append(path)
# print(stack)
#处理栈,当栈为空的时候结束循环
while len(stack) != 0:
#从栈里取出数据
dirPath = stack.pop()
# print(dirPath)
#目录下所有文件
filesList = os.listdir(dirPath)
# print(filesList)
for fileName in filesList:
fileAbsPath = os.path.join(dirPath, fileName)
if os.path.isdir(fileAbsPath):
#是目录就压栈
print("目录: " + fileName)
stack.append(fileAbsPath)
else:
print("普通" + fileName)
getAllDirDE(path = "/Users/guohong/Desktop/guohong")
----------------------------------------
5、队列模拟广度遍历
import os
import collections
def getAllDirQU(path):
queue = collections.deque()
#进队
queue.append(path)
while len(queue) != 0:
#出队数据
dirPath = queue.popleft()
#找出所有的文件
filesList = os.listdir(dirPath)
for fileName in filesList:
#绝对路径
fileAbsPath = os.path.join(dirPath, fileName)
#判断是否是目录,是目录就进队,不是就打印
if os.path.isdir(fileAbsPath):
print("目录: " + fileName)
queue.append(fileAbsPath)
else:
print("普通文件: " + fileName)
getAllDirQU(path = "/Users/guohong/Desktop/guohong")
-----------------------------------
5、示例
处理一个文本文件,找到该文件,把里面的邮箱分类处理
#coding=utf8
import os
import collections
def work(path):
#打开文件
# f = open(path, "r")
resPath = "/Users/guohong/test/day10/res"
with open(path, "r") as f:
while True:
lineInfo = f.readline()
if len(lineInfo) < 5:
break
#邮箱字符串
mailStr = lineInfo.split("---")[0]
print(mailStr)
dirStr = os.path.join(resPath, mailStr.split("@")[1].split(".")[0])
if not os.path.exists(dirStr):
os.rmdir(dirStr)
filePath = os.path.join(dirStr,fileType + ".txt")
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()
#找出所有的文件
filesList = os.listdir(dirPath)
for fileName in filesList:
#绝对路径
fileAbsPath = os.path.join(dirPath, fileName)
#判断是否是目录,是目录就进队,不是就打印
if os.path.isdir(fileAbsPath):
print("目录: " + fileName)
queue.append(fileAbsPath)
else:
#处理普通文件
print("普通" + fileName)
work(fileAbsPath)
getAllDirQU(path="/Users/guohong/test/day10")