開發背景:
電腦的E盤裡有很多電子書,以前對個技術比較感興趣就去下載很多電子書,有些看了,有些沒看,這些電子書沒有在一個地方,於是我準備寫一個腳本,將這個電子書書搜索出來,進行整理一下。
程序設計的思路:
定義一個搜索的根目錄baseDir,一個不搜索的文件夾列表notSearhFolderArr,一個搜索的文件類型列表searchTypeArr,
判斷根目錄baseDir是有效的,並且不存在於notSearhFolderArr數組中,
獲取文件夾下的所有文件及文件夾,
遍歷,判斷子元素是文件就,判斷文件類型是否存在於searchTypeArr,如果存在返回路徑
判斷子元素,是文件夾並且不屬於notSearhFolderArr數組中, 執行第一步,進行遞歸搜索
代碼:
# 根據配置好的文件,搜索文件夾import osimport ioimport sys
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf8')# 主函數baseDir = "E:\\\\Pang\\\\for_search" # 搜索的根目錄notSearchFolderArr = ['node_modules'] # 不搜索的目錄searchFileTypeArr = ['.pdf','.PDF'] # 搜索的文件類型def searhMain():
allResArr = searchFolder(baseDir)
print('\\n'.join(allResArr))# 搜索一個文件目錄 傳入一個文件目錄路徑def searchFolder(folderPath):
folderName = os.path.split(folderPath)[-1]
searFilePathArr = [] if os.path.exists(folderPath) and (folderName not in notSearchFolderArr):
fileArr = os.listdir(folderPath) for item in fileArr:
currentPath = folderPath+'\\\\'+item
(fileName,fileType) = os.path.splitext(item) if os.path.isfile(currentPath) and (fileType in searchFileTypeArr):
searFilePathArr.append(currentPath) if os.path.isdir(currentPath) and (item not in notSearchFolderArr):
innerFileArr = searchFolder(currentPath)
searFilePathArr.extend(innerFileArr) return searFilePathArr
searhMain()
主要用到的模塊和api:
模塊 os: 操作文件的模塊
主要api:
os.path.split : 分割路徑
os.path.exists: 路徑是否存在
os.listdir: 路徑是否是文件夾
os.path.splitext:拆分路徑中的文件擴展名於其他
os.path.isfile: 路徑是否是文件
append: 向數組中追加一個元素
extend: 向數組追加一個數組
運行結果:
程序返回的事根目錄下所有的pdf文件路徑列表
這個腳本稍作修改就可以查詢多個文件類型,對文件進行分類,獲取要查詢的文件路徑後,還可以對文件,進行批量備份到一個文件夾下,或者將搜索結果直接打包壓縮。現在怎麼玩都可以。
閱讀更多 千鋒python學院 的文章