我正在尝试使此函数(搜索给定字符串的目录)也搜索所有子目录,并递归执行此操作。我对 Python 的了解还不够,无法开始。任何指导都会很棒。
谢谢!
def grep(regex, base_dir):
matches = list()
for filename in os.listdir(base_dir):
full_filename = os.path.join(base_dir, filename)
if not os.path.isfile(full_filename):
continue
with open(os.path.join(base_dir, filename)) as fh:
content = fh.read()
matches = matches + re.findall(regex, content)
return matches