python脚本多文件搜索查询字符串关键字
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)
输出:
搜索结束,结果:
/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
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
已为社区贡献16条内容
所有评论(0)