栈
栈是一种内存结构,先进后出,后进先出。python中没有栈的概念,我们目前只能仿写。
# 模拟栈结构
stack = []
# 入栈(添加元素)
stack.append("A")
print(stack)
stack.append("B")
print(stack)
stack.append("C")
print(stack)
#入栈顺序 A B C
# 出栈(移除元素)
stack.pop()
print(stack)
stack.pop()
print(stack)
#出栈顺序 C B A
队列
队列也是一种内存结构,先进先出,后进后出。
创建队列
import collections
queue = collections.deque()
# 进队(向队列中添加元素)
queue.append("A")
queue.append("B")
queue.append("C")
print(queue)
# 出队(移除队列中的元素)
queue.popleft()
print(queue)
queue.popleft()
print(queue)
递归遍历目录和文件
import os
path = r'F:\PycharmProjects\basic gram\作业和习题\test'
def getAllFileAndDir(path):
# 获取当前目录下所有文件及文件目录
fileList = os.listdir(path)
# print(fileList)
# 遍历fileList列表
for fileName in fileList:
# isdir isfile
# print(fileName)
# 拼接绝对路径
absFile = os.path.join(path,fileName)
if os.path.isdir(absFile):
print(absFile+'---目录')
getAllFileAndDir(absFile)
else:
print(absFile+'---文件')
getAllFileAndDir(path)
栈 深度遍历
import collections
def getAllFileAndDir(sourcePath):
stack = collections.deque()
stack.append(sourcePath)
while len(stack) != 0:
path = stack.pop()
fileList = os.listdir(path)
for fileName in fileList:
absFile = os.path.join(path, fileName)
if os.path.isdir(absFile):
print(absFile+'---目录')
stack.append(absFile)
else:
print(absFile+'---文件')
getAllFileAndDir(path)
队列 广度遍历
def getAllFileAndDir(sourcePath):
queue = collections.deque()
queue.append(sourcePath)
while len(queue) !=0:
path = queue.popleft()
fileList = os.listdir(path)
for fileName in fileList:
absFile = os.path.join(path, fileName)
if os.path.isdir(absFile):
print(absFile+'---目录')
queue.append(absFile)
else:
print(absFile+'---文件')
getAllFileAndDir(path)
复制目录和文件
import os
# 复制目录
def copyDir(sourDir,targetDir):
if not os.path.exists(sourDir):
print("如果源目录不存在,直接停止")
return
if not os.path.exists(targetDir):
os.makedirs(targetDir)
listName = os.listdir(sourDir)
for dirNameAndFileName in listName:
sourAbsPath = os.path.join(sourDir,dirNameAndFileName)
targetAbsPath = os.path.join(targetDir,dirNameAndFileName)
if os.path.isdir(sourAbsPath):
copyDir(sourAbsPath,targetAbsPath)
if os.path.isfile(sourAbsPath):
# 如果目标文件不存在, 或者 如果该文件已经存在但是文件大小不一样
if (not os.path.exists(targetAbsPath)) or (os.path.exists(targetAbsPath) and (os.path.getsize(sourAbsPath) != os.path.getsize(targetAbsPath))):
rf = open(sourAbsPath,"rb")
wf = open(targetAbsPath,"wb")
while True:
content = rf.read(1024*1024)
if len(content) == 0:
break
wf.write(content)
wf.flush()
wf.close()
rf.close()
sPath = r'F:\PycharmProjects\basic gram\作业和习题\test'
tPath = r'F:\PycharmProjects\basic gram\作业和习题\testNew'
copyDir(sPath, tPath)
文件复制实例
1.一个函数接受文件夹的名称作为输入参数,请将该文件夹中的所有文件复制到 文件夹名-副本 中去,请补充缺失的代码. (20分)
def copyFile(sPath)
2.题1复制过程中,每隔一秒打印一次复制进度(即当前已复制个数/总文件个数)(15分)
import os
import collections
import time
import sys
def getFileNum(sPath):
num = 0
stack = collections.deque()
stack.append(sPath)
while len(stack) != 0:
path = stack.pop()
fileList = os.listdir(path)
for fileName in fileList:
absFile = os.path.join(path, fileName)
if os.path.isdir(absFile):
stack.append(absFile)
else:
num += 1
return num
def copyFile(sPath):
tPath = r'F:\PycharmProjects\basic gram\作业和习题\Anaconda3-副本'
stack1 = collections.deque()
stack1.append(sPath)
stack2 = collections.deque()
stack2.append(tPath)
timepoint = 1
filenum = 0
while len(stack1) != 0:
sPath = stack1.pop()
tPath = stack2.pop()
if not os.path.exists(tPath):
os.makedirs(tPath)
listName = os.listdir(sPath)
for filename in listName:
absfile = os.path.join(sPath, filename)
tabsfile = os.path.join(tPath, filename)
if os.path.isdir(absfile):
stack1.append(absfile)
stack2.append(tabsfile)
else:
rf = open(absfile, 'rb')
wf = open(tabsfile, 'wb')
while True:
content = rf.read(1024*1024)
if len(content) == 0:
break
wf.write(content)
# 刷新缓冲区
wf.flush()
if time.clock()//1 == timepoint:
sys.stdout.write('\r进度:%d/%d'%(filenum,num))
timepoint += 1
wf.close()
rf.close()
filenum += 1
sys.stdout.write('\r进度:%d/%d' % (num, num))
sPath = r'F:\PycharmProjects\basic gram\作业和习题\Anaconda3'
num = getFileNum(sPath)
# print(num)
start_time = time.clock()
copyFile(sPath)
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。