前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python 对目录的操作

Python 对目录的操作

作者头像
星哥玩云
发布2022-09-08 14:03:20
2540
发布2022-09-08 14:03:20
举报
文章被收录于专栏:开源部署

一、递归遍历目录

代码语言: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:\Users\sunck\Desktop\file"</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:\Users\sunck\Desktop\file"</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:\Users\sunck\Desktop\file"</span>)
print(absPathList)
print(len(absPathList))
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、递归遍历目录
  • 二、栈模拟递归遍历目录
  • 三、队列模拟递归遍历目录
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档