脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - 使用Python遍历文件夹实现查找指定文件夹

使用Python遍历文件夹实现查找指定文件夹

2022-07-28 17:41将冲破艾迪i Python

这篇文章主要为大家介绍了如何使用Python遍历文件夹从而实现查找指定文件夹下所有相同名称的文件、所有相同后缀名的文件,感兴趣的可以了解一下

1. 文件夹结构

指定文件夹:E:/Code/Python/test

指定文件:test.txt

指定文件夹下的目录及文件:

文件夹a:

a.txt

test.txt

文件夹txt:

test.txt

文件b.txt

test.py

test.txt

使用Python遍历文件夹实现查找指定文件夹

使用Python遍历文件夹实现查找指定文件夹

使用Python遍历文件夹实现查找指定文件夹

2. 查找指定文件夹下指定文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import os
 
# 查找该层文件夹下指定文件
def search_file(dirPath, fileName):
    dirs = os.listdir(dirPath) # 查找该层文件夹下所有的文件及文件夹,返回列表
    for currentFile in dirs: # 遍历列表
        absPath = dirPath + '/' + currentFile # 文件的绝对路径
        if currentFile == fileName:
             print(absPath)
           
if __name__ == "__main__":
    dirPath = 'E:/Code/Python/test'
    fileName = 'test.txt'
    search_file(dirPath, fileName)
             

执行结果:

E:/Code/Python/test/test.txt

3. 查找指定文件夹下所有相同名称的文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import os
 
# 查找指定文件夹下所有相同名称的文件
def search_file(dirPath, fileName):
    dirs = os.listdir(dirPath) # 查找该层文件夹下所有的文件及文件夹,返回列表
    for currentFile in dirs: # 遍历列表
        absPath = dirPath + '/' + currentFile
        if os.path.isdir(absPath): # 如果是目录则递归,继续查找该目录下的文件
            search_file(absPath, fileName)
        elif currentFile == fileName:
            print(absPath) # 文件存在,则打印该文件的绝对路径
 
if __name__ == "__main__":
    dirPath = 'E:/Code/Python/test'
    fileName = 'test.txt'
    search_file(dirPath, fileName)

执行结果:

E:/Code/Python/test/a/test.txt
E:/Code/Python/test/test.txt    
E:/Code/Python/test/txt/test.txt

4. 查找指定文件夹下所有相同后缀名的文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import os
 
# 查找指定文件夹下所有相同后缀名的文件
def search_file_suffix(dirPath, suffix):
    dirs = os.listdir(dirPath)
    for currentFile in dirs:
        absPath = dirPath + '/' + currentFile
        if os.path.isdir(absPath):
            search_file_suffix(absPath, suffix)
        elif currentFile.split('.')[-1] == suffix: # 文件后缀名相同,则打印该文件的绝对路径
            print(absPath)
 
if __name__ == "__main__":
    dirPath = 'E:/Code/Python/test'
    suffix = 'txt'
    search_file_suffix(dirPath, suffix)
     

执行结果:

E:/Code/Python/test/a/a.txt
E:/Code/Python/test/a/test.txt  
E:/Code/Python/test/b.txt       
E:/Code/Python/test/test.txt    
E:/Code/Python/test/txt/test.txt

以下是需要注意的避坑点:

1.注意避坑 - 递归函数的位置:查找相同后缀名的文件时,递归调用必须放在if里,而不能放在elif里,否则当文件夹名称与文件后缀名相同时,会出错。

如:查找txt后缀的文件,刚好又有一个txt的文件夹,因为递归放在了elif里,那么遍历会停在txt文件夹,而不去遍历txt文件夹里的内容。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import os
 
# 查找指定文件夹下所有相同后缀名的文件
def search_file_suffix(dirPath, suffix):
    dirs = os.listdir(dirPath)
    for currentFile in dirs:
        absPath = dirPath + '/' + currentFile
        if os.path.isdir(absPath):
            search_file_suffix(absPath, suffix)
        elif currentFile.split('.')[-1] == suffix: # 文件后缀名相同,则打印该文件的绝对路径
            print(absPath)
 
def search_file_suffix_temp(dirPath, suffix):
    dirs = os.listdir(dirPath)
    for currentFile in dirs:
        absPath = dirPath + '/' + currentFile
        if currentFile.split('.')[-1] == suffix:
            print(absPath)
        elif os.path.isdir(absPath): # 递归判断放在elif里
            search_file_suffix_temp(absPath, suffix)
 
if __name__ == "__main__":
    dirPath = 'E:/Code/Python/test'
    suffix = 'txt'
    search_file_suffix(dirPath, suffix)
    print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    search_file_suffix_temp(dirPath, suffix)

执行结果:

E:/Code/Python/test/a/a.txt
E:/Code/Python/test/a/test.txt  
E:/Code/Python/test/b.txt       
E:/Code/Python/test/test.txt    
E:/Code/Python/test/txt/test.txt
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
E:/Code/Python/test/a/a.txt     
E:/Code/Python/test/a/test.txt  
E:/Code/Python/test/b.txt       
E:/Code/Python/test/test.txt
E:/Code/Python/test/txt

2.注意避坑 - .vscode文件夹:在使用vscode打开文件夹时,会自动生成.vscode文件夹,如果查找文件的时候不想查找这些配置文件夹里的文件,那么需要在判断文件夹后,再加一层判断,遇到.vscode或者其他配置文件夹就跳过。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import os
from fnmatch import fnmatchcase
 
# 查找指定文件夹下所有相同后缀名的文件
def search_file_suffix(dirPath, suffix):
    dirs = os.listdir(dirPath)
    for currentFile in dirs:
        absPath = dirPath + '/' + currentFile
        if os.path.isdir(absPath):
            search_file_suffix(absPath, suffix)
        elif currentFile.split('.')[-1] == suffix: # 文件后缀名相同,则打印该文件的绝对路径
            print(absPath)
 
def search_file_suffix_temp(dirPath, suffix):
    dirs = os.listdir(dirPath)
    for currentFile in dirs:
        absPath = dirPath + '/' + currentFile
        if os.path.isdir(absPath):
            if fnmatchcase(currentFile,'.*'): # 如果文件夹是.vscode或者其他.开头的文件夹,就跳过
                pass
            else:
                search_file_suffix(absPath, suffix)
        elif currentFile.split('.')[-1] == suffix:
            print(absPath)
 
if __name__ == "__main__":
    dirPath = 'E:/Code/python/test'
    suffix = 'json'
    search_file_suffix(dirPath, suffix)
    print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    search_file_suffix_temp(dirPath, suffix)   

执行结果:

E:/Code/python/test/.vscode/settings.json
E:/Code/python/test/a.json
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
E:/Code/python/test/a.json

到此这篇关于使用Python遍历文件夹实现查找指定文件夹的文章就介绍到这了,更多相关Python查找指定文件夹内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/aidijava/article/details/125828429

延伸 · 阅读

精彩推荐
  • Pythonpython3 遍历删除特定后缀名文件的方法

    python3 遍历删除特定后缀名文件的方法

    下面小编就为大家分享一篇python3 遍历删除特定后缀名文件的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    nudt_qxx10952021-02-04
  • Pythonpython支持多继承吗

    python支持多继承吗

    在本篇文章里小编给大家整理的是关于python支持多继承的相关基础知识点,需要的朋友们跟着学习参考下。...

    silencement7602020-06-20
  • Pythonpython判断图片宽度和高度后删除图片的方法

    python判断图片宽度和高度后删除图片的方法

    这篇文章主要介绍了python判断图片宽度和高度后删除图片的方法,涉及Python中os模块与Image模块的相关使用技巧,需要的朋友可以参考下...

    八大山人6312020-07-05
  • PythonPython实现SVN的目录周期性备份实例

    Python实现SVN的目录周期性备份实例

    这篇文章主要介绍了Python实现SVN的目录周期性备份,实例分析了Python实现SVN周期性备份的原理与实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下...

    优雅先生2562020-07-24
  • PythonPython解析树及树的遍历

    Python解析树及树的遍历

    本篇是给大家介绍的Python实现解析树以及实现二叉树的三种遍历,先序遍历,中序遍历,后序遍历的例子,非常的详细,有需要的小伙伴可以参考下。 ...

    Python教程网2862020-08-11
  • PythonDjango自定义分页效果

    Django自定义分页效果

    这篇文章主要为大家详细介绍了Django自定义分页效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    jack-boy4002020-11-21
  • PythonPandas高级教程之Pandas中的GroupBy操作

    Pandas高级教程之Pandas中的GroupBy操作

    通常来说groupby操作可以分为三部分:分割数据,应用变换和和合并数据,本文将会详细讲解Pandas中的groupby操作,感兴趣的朋友一起看看吧...

    flydean程序那些事7532021-12-13
  • Python在Python中使用sort()方法进行排序的简单教程

    在Python中使用sort()方法进行排序的简单教程

    这篇文章主要介绍了在Python中使用sort()方法进行排序的简单教程,是Python学习中的基础知识,需要的朋友可以参考下 ...

    脚本之家6922020-07-04