Python递归搜索文件夹中的每个文件,打开它们看有没有包含关键词,返回一个列表.

"""
递归搜索文件夹中的每个文件,打开它们看有没有包含关键词,返回一个列表.
"""
import os


def predict_encoding(file_path, n_lines=20):
    '''Predict a file's encoding using chardet'''
    import chardet

    # 用二进制只读方式打开文件,探测文件编码
    with open(file_path, 'rb') as f:
        # 连接指定的行数
        rawdata = b''.join([f.readline() for _ in range(n_lines)])

    return chardet.detect(rawdata)['encoding']

def searchinfile(keywords,filename):
    """在文件中搜索有没有包含关键词,有则返回True"""
    contain_flag=False
    try:
       e = predict_encoding(filename)
       f = open(filename, mode='r',encoding=e)
       for line in f:
          if  keywords in line:
              contain_flag=True
              break
       f.close()            
    except:
       pass

    return contain_flag

def searchinfolder(keywords,foldername):
    """在文件夹中搜索每个文件,打开它查找有没有关键词"""
    
    resultlist=[]
    if os.path.isdir(foldername):  
        for item in os.walk(foldername):        #返回的是三元组
            for eachfile in item[2]:
                afile=item[0] + "\\" + eachfile
                #print(afile)
                if searchinfile(keywords,afile):
                    resultlist.append(afile)
    return resultlist
                
if __name__=="__main__":
    
    目录 = "e:/www.scratch8.net/python"
    keywords="""nav-previous"""

    items = searchinfolder(keywords,目录)

    for file in items:
        print(file)