Python遍历目录并查找文件内容

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010889616/article/details/78946568

在做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()
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页