使用Python遍歷文件夾實(shí)現(xiàn)查找指定文件夾
1. 文件夾結(jié)構(gòu)
指定文件夾:E:/Code/Python/test
指定文件:test.txt
指定文件夾下的目錄及文件:
文件夾a:
a.txt
test.txt
文件夾txt:
test.txt
文件b.txt
test.py
test.txt
2. 查找指定文件夾下指定文件
import os # 查找該層文件夾下指定文件 def search_file(dirPath, fileName): dirs = os.listdir(dirPath) # 查找該層文件夾下所有的文件及文件夾,返回列表 for currentFile in dirs: # 遍歷列表 absPath = dirPath + '/' + currentFile # 文件的絕對(duì)路徑 if currentFile == fileName: print(absPath) if __name__ == "__main__": dirPath = 'E:/Code/Python/test' fileName = 'test.txt' search_file(dirPath, fileName)
執(zhí)行結(jié)果:
E:/Code/Python/test/test.txt
3. 查找指定文件夾下所有相同名稱的文件
import os # 查找指定文件夾下所有相同名稱的文件 def search_file(dirPath, fileName): dirs = os.listdir(dirPath) # 查找該層文件夾下所有的文件及文件夾,返回列表 for currentFile in dirs: # 遍歷列表 absPath = dirPath + '/' + currentFile if os.path.isdir(absPath): # 如果是目錄則遞歸,繼續(xù)查找該目錄下的文件 search_file(absPath, fileName) elif currentFile == fileName: print(absPath) # 文件存在,則打印該文件的絕對(duì)路徑 if __name__ == "__main__": dirPath = 'E:/Code/Python/test' fileName = 'test.txt' search_file(dirPath, fileName)
執(zhí)行結(jié)果:
E:/Code/Python/test/a/test.txt
E:/Code/Python/test/test.txt
E:/Code/Python/test/txt/test.txt
4. 查找指定文件夾下所有相同后綴名的文件
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: # 文件后綴名相同,則打印該文件的絕對(duì)路徑 print(absPath) if __name__ == "__main__": dirPath = 'E:/Code/Python/test' suffix = 'txt' search_file_suffix(dirPath, suffix)
執(zhí)行結(jié)果:
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
以下是需要注意的避坑點(diǎn):
1.注意避坑 - 遞歸函數(shù)的位置:查找相同后綴名的文件時(shí),遞歸調(diào)用必須放在if里,而不能放在elif里,否則當(dāng)文件夾名稱與文件后綴名相同時(shí),會(huì)出錯(cuò)。
如:查找txt后綴的文件,剛好又有一個(gè)txt的文件夾,因?yàn)檫f歸放在了elif里,那么遍歷會(huì)停在txt文件夾,而不去遍歷txt文件夾里的內(nèi)容。
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: # 文件后綴名相同,則打印該文件的絕對(duì)路徑 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)
執(zhí)行結(jié)果:
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打開(kāi)文件夾時(shí),會(huì)自動(dòng)生成.vscode文件夾,如果查找文件的時(shí)候不想查找這些配置文件夾里的文件,那么需要在判斷文件夾后,再加一層判斷,遇到.vscode或者其他配置文件夾就跳過(guò)。
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: # 文件后綴名相同,則打印該文件的絕對(duì)路徑 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或者其他.開(kāi)頭的文件夾,就跳過(guò) 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)
執(zhí)行結(jié)果:
E:/Code/python/test/.vscode/settings.json
E:/Code/python/test/a.json
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
E:/Code/python/test/a.json
到此這篇關(guān)于使用Python遍歷文件夾實(shí)現(xiàn)查找指定文件夾的文章就介紹到這了,更多相關(guān)Python查找指定文件夾內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
torchtext入門教程必看,帶你輕松玩轉(zhuǎn)文本數(shù)據(jù)處理
這篇文章主要介紹了torchtext入門教程必看,帶你輕松玩轉(zhuǎn)文本數(shù)據(jù)處理,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05Python大批量寫入數(shù)據(jù)(百萬(wàn)級(jí)別)的方法
這篇文章主要給大家介紹了關(guān)于Python大批量寫入數(shù)據(jù)(百萬(wàn)級(jí)別)的相關(guān)資料,在日常處理數(shù)據(jù)的過(guò)程中,我們都有批量寫入數(shù)據(jù)的需求,文中給出了詳細(xì)的示例代碼,需要的朋友可以參考下2023-07-07Python圖像閾值化處理及算法比對(duì)實(shí)例解析
這篇文章主要介紹了Python圖像閾值化處理及算法比對(duì)實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06python bluetooth藍(lán)牙信息獲取藍(lán)牙設(shè)備類型的方法
這篇文章主要介紹了python bluetooth藍(lán)牙信息獲取藍(lán)牙設(shè)備類型的方法,具體轉(zhuǎn)化方法文中給大家介紹的非常詳細(xì),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11Python把excel文件數(shù)據(jù)轉(zhuǎn)化為字典格式存儲(chǔ)詳解
這篇文章主要介紹了Python把excel文件數(shù)據(jù)轉(zhuǎn)化為字典格式存儲(chǔ)詳解,在Python中有時(shí)候需要操作excel表格的數(shù)據(jù),把excel表格轉(zhuǎn)化為字典存起來(lái),方便讀取,今天我們就來(lái)看看如何轉(zhuǎn)換,需要的朋友可以參考下2023-08-08Python+seaborn實(shí)現(xiàn)聯(lián)合分布圖的繪制
聯(lián)合分布(Joint Distribution)圖是一種查看兩個(gè)或兩個(gè)以上變量之間兩兩相互關(guān)系的可視化圖,在數(shù)據(jù)分析操作中經(jīng)常需要用到。本文將通過(guò)seaborn實(shí)現(xiàn)繪制聯(lián)合分布圖,需要的可以參考一下2023-02-02Python獲取數(shù)據(jù)庫(kù)數(shù)據(jù)并保存在excel表格中的方法
今天小編就為大家分享一篇Python獲取數(shù)據(jù)庫(kù)數(shù)據(jù)并保存在excel表格中的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06