Python 对目录的操作

2022-09-08 14:03:20 浏览数 (1)

一、递归遍历目录

代码语言:javascript复制
<span class="hljs-comment"># 返回所有文件的绝对路径</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">traverseDir</span><span class="hljs-params">(dirPath)</span>:</span>
    absPathList = []
    <span class="hljs-keyword">import</span> os

    filesList = os.listdir(dirPath)
    <span class="hljs-keyword">for</span> fileName <span class="hljs-keyword">in</span> filesList:
        absPath = os.path.join(dirPath, fileName)
        <span class="hljs-keyword">if</span> os.path.isdir(absPath):
            <span class="hljs-comment"># 目录</span>
            absPathList  = traverseDir(absPath)
        <span class="hljs-keyword">else</span>:
            <span class="hljs-comment"># 文件</span>
            <span class="hljs-comment"># print(absPath)</span>
            absPathList.append(absPath)
    <span class="hljs-keyword">return</span> absPathList

absPathList = traverseDir(<span class="hljs-string">r"C:UserssunckDesktopfile"</span>)
print(absPathList)
print(len(absPathList))
<span class="hljs-comment"># for absPath in absPathList:</span>
<span class="hljs-comment">#     print(absPath)</span>

二、栈模拟递归遍历目录

代码语言:javascript复制
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">traverseDir</span><span class="hljs-params">(dirPath)</span>:</span>
    <span class="hljs-keyword">import</span> os
    absPathList = []
    myStack = []
    myStack.append(dirPath)

    <span class="hljs-keyword">while</span> len(myStack) != <span class="hljs-number">0</span>:
        path = myStack.pop()

        fileList = os.listdir(path)
        <span class="hljs-keyword">for</span> fileName <span class="hljs-keyword">in</span> fileList:
            absPath = os.path.join(path, fileName)
            <span class="hljs-keyword">if</span> os.path.isdir(absPath):
                myStack.append(absPath)
            <span class="hljs-keyword">else</span>:
                absPathList.append(absPath)
    <span class="hljs-keyword">return</span> absPathList


absPathList = traverseDir(<span class="hljs-string">r"C:UserssunckDesktopfile"</span>)
print(absPathList)
print(len(absPathList))

三、队列模拟递归遍历目录

代码语言:javascript复制
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">traverseDir</span><span class="hljs-params">(dirPath)</span>:</span>
    <span class="hljs-keyword">import</span> os
    <span class="hljs-keyword">from</span> collections <span class="hljs-keyword">import</span> deque
    absPathList = []

    q = deque([])
    q.append(dirPath)

    <span class="hljs-keyword">while</span> len(q) != <span class="hljs-number">0</span>:
        path = q.popleft()

        fileList = os.listdir(path)
        <span class="hljs-keyword">for</span> fileName <span class="hljs-keyword">in</span> fileList:
            absPath = os.path.join(path, fileName)
            <span class="hljs-keyword">if</span> os.path.isdir(absPath):
                q.append(absPath)
            <span class="hljs-keyword">else</span>:
                absPathList.append(absPath)
                <span class="hljs-comment"># yield absPath</span>
    <span class="hljs-keyword">return</span> absPathList

absPathList = traverseDir(<span class="hljs-string">r"C:UserssunckDesktopfile"</span>)
print(absPathList)
print(len(absPathList))

0 人点赞