Python遍歷目錄並查詢檔案內容
阿新 • • 發佈:2019-01-27
在做Android逆向的時候,dex檔案轉成smail檔案之後,需要查詢哪個smail檔案中包含的關鍵字。下面這段程式碼:
findCount為查詢到的檔案個數
findId為要查詢的內容
findDir為要查詢的資料夾
#encoding: utf-8 import os findCount = 0 findId = "QString" findDir = "F:\\calc" resultFile = os.path.join(findDir,"result.txt") def writeResultAndPrint(fullPath): print (fullPath) file = open(resultFile,'a') file.write(fullPath) file.write("\n") file.close() def findKey(fullPath): file = open(fullPath,'r') content = file.read() file.close() isExist = content.find(findId) if isExist > 0: global findCount findCount = findCount + 1 writeResultAndPrint(fullPath) def findFiles(): for dirPath,dirNames,fileNames in os.walk(findDir): for file in fileNames: fullPath = os.path.join(dirPath,file) findKey(fullPath) print("找到了字串個數=" + str(findCount)) def clean(): if os.path.exists(resultFile): os.remove(resultFile) clean() findFiles()