import os
import re
def findKey(filePath, fileReg, key):
for filepath,dirnames,filenames in os.walk(filePath):
for filename in filenames:
try:
_path = os.path.join(filepath,filename)
if re.search(fileReg, _path) != None:
with open(_path, 'r') as f:
string = f.read()
find = string.find(key)
if find != -1:
print('find:')
print(_path)
f.close()
except:
#
print('err:')
print(str(_path))
if __name__ == '__main__':
rootdir = './src'
# key = 'export class ChromeDocumentTitleManager'
key = 'export class ExampleComponent implements OnInit'
file_reg = r'\.ts$'
findKey(rootdir, file_reg, key)
python: 从文件夹下所有文件中(包括子子子文件夹),找到包含关键字的文件
于 2021-11-27 00:28:28 首次发布
该代码段展示了一个Python脚本,它遍历指定目录`./src`下的所有.ts文件,使用正则表达式匹配文件名,并读取文件内容来查找关键字`exportclassExampleComponentimplementsOnInit`。如果找到匹配项,它会打印文件路径。这个脚本对于在项目源代码中查找特定导出组件非常有用。

2073

被折叠的 条评论
为什么被折叠?



