文件目录遍历

递归遍历目录和文件

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)
    原文作者:rottengeek
    原文地址: https://segmentfault.com/a/1190000014691354
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞