#1、写一个函数,传入一个路径和一个关键字(关键字是文件内容),找到文件内容里面有这个关键字的文件
import os
def find_file(keywords,path=\'/Users/wym/Desktop/work/Besttest/笔记/day6\'):
file_list = get_all_file(path)
for file in file_list:
if file.endswith(\'.py\') or file.endswith(\'.txt\'):
with open(file,\'r\',encoding=\'utf-8\') as fr:
if keywords in fr.read():
print(file)
print(\'==\'*20)
def get_all_file(path):
file_list = []
for cur_path, cur_dirs, cur_files in os.walk(path):
for name in cur_files:
file_list.append(os.path.join(cur_path,name))
return file_list
keywords = input("Please input the key words that you want to search:")
path = input("Please input the filepath:")
find_file(keywords,path)
相关文章: