import os
import re
import pandas as pd
# print(os.walk())
def find_keyword(key_word):
data = {'内容':[],'行数':[],'位置':[]}
for filepath, dirnames, filenames in os.walk(os.getcwd()):
for filename in filenames:
file_absolute = filepath + '/' + filename
# print(file_absolute)
with open(file_absolute, 'rb') as fa:
content = fa.readlines()
for i, content_target in enumerate(content):
# print(content_target)
if re.findall(key_word.encode(), content_target, re.S):
data['内容'].append(content_target)
data['行数'].append(i)
data['位置'].append(file_absolute)
m = pd.Dataframe(data)
m.to_excel('result.xlsx')
1.使用脚本时把该python文件放在同目录文件夹下,它会扫描该目录下以及子目录下的所有文件最后把查找到的关键字、文件、行数返回生成result.xlxl的excel文件



