Python查詢檔案有多牛?男默女淚!!!

某天,隔壁大神正在看一份核心技術文件,我想研讀下文件向大神“偷師”,恰好我的虛擬XP桌面無法使用搜索功能,而且專案組一般是一起使用共享目錄的,這就導致大量資料雜糅在一起,我要找到大神的"葵花寶典"難上加難。
在日常工作中,大家基本都要自己記錄重要檔案路徑,或者通過一層又一層目錄去查詢需要的檔案,這不僅大海撈針,而且相當費時!
今天就為大家分享如何使用Python查詢你最需要的檔案,分為四個版本(初級、中級、高階、傳說),預設以Win形式演示。
1、介紹os.walk遍歷目錄樹
在實際工作中,極有可能會遇到查詢某個目錄及其子目錄下的所有檔案。例如,查詢某個目錄及其目錄下的所有的圖片檔案或者所有的Word檔名,查詢某個目錄及其子目錄最大的十個檔案。對於每個目錄而言,os.walk返回一個三元組(dirpath,dirnames,filenames)。
dirpath:儲存的是當前目錄。 dirnames:當前目錄下的子目錄列表 filenames: 當前目錄下的檔案列表。
2、查詢檔案程式碼
本次預設Windows查詢檔案,演示的圖片如下
主目錄:F:python演示流浪地球計劃

子目錄:F:python演示流浪地球計劃總計劃

1. 初級查詢檔案(findfile_v1.py)
1import os 2 3def getFilepath(rootDir): 4 filepathresult = [] # 所需要的檔案路徑的集合 5 for dirpath,dirNames,fileNames in os.walk(rootDir): 6 for fileName in fileNames: 7 apath = os.path.join(dirpath, fileName) 8 filepathresult.append(apath) 9 for i in range(len(filepathresult)): 10 print(filepathresult[i]) 11 12if __name__ == '__main__': 13 rootDir = r'F:python演示流浪地球計劃\' 14 getFilepath(rootDir)
1.1 重點程式碼詳解
1)執行流
rootDir:根目錄(即需要查詢的目錄)
2)第7行 apath :檔案的絕對路徑
os.path.join(dirpath ,fileName) : 將當前目錄和檔名組合
1.2 初級查詢檔案優缺點
優點:便捷,可以把該目錄所有檔案都查詢出來。
缺點:無法根據檔名關鍵字或者指定檔案型別進行篩選。
1.3 執行演示
初級查詢檔案findfile_v1.py確實是沒有在帶任何過濾的情況下,把該目錄的所有檔案都查詢出來了。

2. 中級查詢檔案(findfile_v2.py)
1import os 2 3def getFilepath(rootDir,filepathmsg,filetype): 4 filepathresult = [] 5 for dirpath,dirNames,fileNames in os.walk(rootDir): 6 for fileName in fileNames: 7 apath = os.path.join(dirpath ,fileName) 8 apathname = os.path.splitext(apath)[0] 9 apathtype = os.path.splitext(apath)[1] 10 if filetype == apathtype: 11 if filepathmsg in apathname: 12 filepathresult.append(apath) 13 for i in range(len(filepathresult)): 14 print(filepathresult[i]) 15 16if __name__ == '__main__': 17 rootDir = r'F:python演示流浪地球計劃\' 18 filepathmsg = "2019年" 19 filetype = ".txt" 20 getFilepath(rootDir,filepathmsg,filetype)
2.1 重點程式碼詳解
1)執行流
filepathmsg :檔名關鍵資訊(字串型別)
filetype :副檔名(字串型別)
- 第4行 filepathresult :為所需要的檔案路徑的集合
3)第8行 apathname = os.path.splitext(apath)[0]
4)第9行 apathtype = os.path.splitext(apath)[1]
os.path.splitext:返回一個除去副檔名的部分和副檔名的二元組
apathname:為路徑+檔名,不包括副檔名
apathtype :為檔案型別,得到"."+ "副檔名"
2.2 中級查詢檔案優缺點
優點:搜尋唯一指定檔名關鍵字加指定的檔案型別。
缺點:無法搜尋多個檔名關鍵字或者多個指定的檔案型別。
2.3 執行演示
中級查詢檔案findfile_v2.py,過濾資訊為檔名資訊"2019年"和檔案型別".txt",符合該過濾資訊的檔名(含子目錄)都被篩選出來。有個要點,老鐵們要注意,如果檔案路徑符合檔名關鍵字資訊,也是會被篩選出來的!因為apathname包含路徑和檔名(即除副檔名)。

3. 高階查詢檔案(findfile_v3.py)
1import os 2 3def getFilepath(rootDir,filepathmsg,filetype): 4 filepathresult = [] 5 for dirpath,dirNames,fileNames in os.walk(rootDir): 6 for fileName in fileNames: 7 apath = os.path.join(dirpath ,fileName) 8 apathname = os.path.splitext(apath)[0] 9 apathtype = os.path.splitext(apath)[1] 10 for i in filetype: 11 if i in apathtype: 12 for j in filepathmsg: 13 if j in apathname: 14 filepathresult.append(apath) 15 for i in range(len(filepathresult)): 16 print(filepathresult[i]) 17 18if __name__ == '__main__': 19 rootDir = r'F:python演示流浪地球計劃\' 20 filetype = [".TXT",".xlsx",".docx",".txt"] 21 filepathmsg = ["流浪時代II","新太陽時代","逃逸時代"] 22 getFilepath(rootDir,filepathmsg,filetype)
3.1 重點程式碼詳解
1)執行流
filepathmsg :字串(中級版)轉變為陣列(高階版)
filetype :字串類(中級版)轉變為陣列(高階版)
2)第10-11行 迴圈判斷體(檔案型別)
3)第12-13行 迴圈判斷體(檔案路徑/檔名)
3.2 高階查詢檔案優缺點
優點:可以進行多個檔名關鍵資訊和多個副檔名進行聯合查詢。
缺點:若搜尋更廣的範圍,查詢時間就會更長。
3.3 執行演示
3.3.1 執行原始碼
執行原始碼可以發現符合檔名關鍵字資訊和檔案型別都查詢出來了,沒有docx的出現是因為沒有符合檔名關鍵字資訊。那究竟誰才是主,誰才是次呢?

3.3.2 修改邏輯演示
將上述執行流程式碼修改如下:
1filetype = [".TXT",".xlsx",".docx",".txt"] 2filepathmsg = ["流浪時代II","新太陽時代","逃逸時代","第三階段"]
執行程式碼,可以發現子目錄的"2019年第三階段.zip"沒有被查詢出來,可是檔名關鍵字資訊不是有"第三階段"嗎?怎麼回事?老鐵們如果這裡有點懵了,可以回看程式碼。第一次執行迴圈判斷體是檔案型別,第二次執行迴圈判斷體是關鍵檔名資訊。如果第一次判斷都不符合,就不會進入第二迴圈判斷體了。
設計思路:如果你想查詢更多檔案型別,那filetype少填寫,不就OK了嗎?如果你知道要的是什麼檔案型別,那就多填寫幾個!

3、Linux演示
演示的檔案如下,和Win的演示大致相同。

1 初級查詢
1(pyOracle) [python@PYMY-DDB findfile]$ pwd 2/home/python/pyOracle/findfile 3(pyOracle) [python@PYMY-DDB findfile]$ cat findfile_v1.py 4#!/usr/bin/python 5# coding=utf8 6import os 7 8def getFilepath(rootDir): 9 filepathresult = [] 10 for dirpath,dirNames,fileNames in os.walk(rootDir): 11 for fileName in fileNames: 12 apath = os.path.join(dirpath, fileName) 13 filepathresult.append(apath) 14 for i in range(len(filepathresult)): 15 print(filepathresult[i]) 16if __name__ == '__main__': 17 rootDir = r'/home/python/pyOracle/wechat/流浪地球計劃//' 18 getFilepath(rootDir)
初級查詢演示結果

2 中級查詢
1(pyOracle) [python@PYMY-DDB findfile]$ pwd 2/home/python/pyOracle/findfile 3(pyOracle) [python@PYMY-DDB findfile]$ cat findfile_v2.py 4 5#!/usr/bin/python 6# coding=utf8 7import os 8def getFilepath(rootDir,filepathmsg,filetype): 9 filepathresult = [] 10 #for maindir,subdir,file_name_list in os.walk(dirpath): 11 for dirpath,dirNames,fileNames in os.walk(rootDir): 12 for fileName in fileNames: 13 apath = os.path.join(dirpath ,fileName) 14 apathname = os.path.splitext(apath)[0] 15 apathtype = os.path.splitext(apath)[1] 16 if filetype == apathtype: 17 if filepathmsg in apathname: 18 filepathresult.append(apath) 19 for i in range(len(filepathresult)): 20 print(filepathresult[i]) 21if __name__ == '__main__': 22 filepathmsg = "2019年" 23 rootDir = r'/home/python/pyOracle/wechat/流浪地球計劃//' 24 filetype = ".txt" 25 getFilepath(rootDir, filepathmsg, filetype)
中級查詢演示結果

3 高階查詢
1(pyOracle) [python@PYMY-DDB findfile]$ pwd 2/home/python/pyOracle/findfile 3(pyOracle) [python@PYMY-DDB findfile]$ cat findfile_v3.py 4#!/usr/bin/python 5# coding=utf8 6import os 7 8def getFilepath(rootDir,filepathmsg,filetype): 9 filepathresult = [] 10 for dirpath,dirNames,fileNames in os.walk(rootDir): 11 for fileName in fileNames: 12 apath = os.path.join(dirpath ,fileName) 13 apathname = os.path.splitext(apath)[0] 14 apathtype = os.path.splitext(apath)[1] 15 #for i in range(len(filetype)): 16 for i in filetype: 17 if i in apathtype: 18 #for j in range(len(filepathmsg)): 19 for j in filepathmsg: 20 if j in apathname: 21 filepathresult.append(apath) 22 for i in range(len(filepathresult)): 23 print(filepathresult[i]) 24 25if __name__ == '__main__': 26 rootDir = r'/home/python/pyOracle/wechat/流浪地球計劃//' 27 filetype = [".TXT",".xlsx",".doc",".txt"] 28 filepathmsg = ["流浪時代II","新太陽時代","逃逸時代","第三階段"] 29 getFilepath(rootDir,filepathmsg,filetype)
高階查詢演示結果

細心的老鐵有沒有發現,為什麼rootDir最後面的路徑多要加""(Win)或”//“(Linux),其實是為了轉義。
可能你會問,找檔案用python幹嘛?當然是方便老鐵們找羞羞的電影入門學習啦!你把資料夾藏起來都沒有用,它都能到喔!
歡迎大家加入小編建立的Python行業交流群,有大牛答疑,有資源共享,有企業招人!是一個非常不錯的交流基地!群號:556370268
