棧是一種內存結構,先進後出,後進先出。python中沒有棧的概念,咱們目前只能仿寫。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
隊列也是一種內存結構,先進先出,後進後出。app
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)