当前位置:   article > 正文

python脚本多文件搜索查询字符串关键字_使用脚本搜索文件夹里的文件的关键字

使用脚本搜索文件夹里的文件的关键字

服务器上要搜索某个目录下的所有文件中哪些文件含有某一字符串关键字,可用该脚本。

示例:
/usr/local 目录下(包含所有子目录下)的所有可读文件中搜索哪些含有字符串 "if name== "

说明:
1.本脚本只会搜索可读文本文件,list中定义的类型文件会跳过不搜索
2.如果想修改搜索的文件路径和查找文件中的字符串请修改32行的dir和33行的searchWord

脚本如下:

#!/usr/bin/env python
import os

list = ["jar", "zip", "rar", "7z", "tar", "gzip", "gz", "xz", "bz2", "doc", "class", "pak", "webm", "appx",
            "xls", "ppt", "pdf", "ofd", "docx", "xlsx", "pptx", "jpg", "jpge", "gif", "png", "ett", "ram",
            "xltd", "war", "hprof", "m4a", "swf", "mobi", "jpeg", "tif", "tiff", "svg", "psd", "eps", "qsv",
            "mp3", "aac", "mp4", "avi", "flv", "mkv", "mpeg", "msi", "tgz", "mdf", "xlsm", "rm", "ogg", "mod",
            "rmvb", "apk", "ts", "map", "car", "mov", "wav", "raw", "dll", "woff", "igs", "dwt", "dng", "msix",
            "eot", "otf", "ico", "ttf", "ttc", "fon", "dl_", "pd_", "ex_", "etl", "dwf", "iges", "wpt", "cer",
            "sys", "iso", "isz", "esd", "wim", "gho", "dmg", "mpf", "exe", "ldf", "mpg", "3dm", "fbx", "bin",
            "wmv", "3gp", "drawio", "dcm", "tga", "bmp", "jfif", "webp", "dwg", "dxf", "vsd", "vsdx", "api",
            "ifc", "dwfx", "stl", "cf2", "plt", "obj", "3ds", "stl", "ply", "gltf", "glb", "off", "et", "mpp",
            "dae", "wrl", "3mf", "ifc", "brep", "step", "iges", "fcstd", "bim", "epub", "wmf", "emf", "CHS",
            "xmind", "odt", "ods", "ots", "odp", "otp", "six", "ott", "fodt", "fods", "wps", "dps", "x3d"]
result = dict()

def search(dir, word):
    if os.path.isdir(dir):
        for filename in os.listdir(dir):
            file_address = os.path.join(dir, filename)
            if os.path.isfile(file_address):
                fileType = os.path.splitext(filename)[-1]
                if fileType not in list:
                    print("正在搜索文件:%s" % file_address)
                    f = open(file_address, 'r', encoding='utf-8', errors='ignore')
                    fileInfo = []
                    i = 1
                    for info in f.readlines():
                        if info.find(word) > -1:
                            fileInfo.append("第" + str(i) + "行:" + info.strip('\n'))
                        i += 1
                    if len(fileInfo) > 0:
                        result[file_address] = fileInfo
            else:
                search(file_address, word)


if __name__ == "__main__":
    dir = "/usr/local"
    searchWord = "if __name__=="
    search(dir, searchWord)
    print()
    print("搜索结束,结果:")
    for k, v in result.items():
        print()
        print(k)
        for info in v:
            print(info)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

输出:

搜索结束,结果:

/usr/local/lib/python3.5/trace.py
第855行:if __name__=='__main__':

/usr/local/lib/python3.5/wsgiref/__pycache__/simple_server.cpython-35.opt-1.pyc
第19行:For example usage, see the 'if __name__=="__main__"' block at the end of the

/usr/local/lib/python3.5/wsgiref/__pycache__/simple_server.cpython-35.pyc
第19行:For example usage, see the 'if __name__=="__main__"' block at the end of the

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/319531
推荐阅读
相关标签
  

闽ICP备14008679号