作业-查找文件内容



#1、获取到某个目录下面所有的文件
#2、判断是否以.txt结尾
#3、如果是.txt结尾打开这个文件,读取内容
#4、判断这个关键字是否在文件内容里面
#5、如果在的话 就输出
import os


def find_content(keyword,path):
if os.path.exists(path):
for cur_path,dirs,files in os.walk(path):
for file in files:
if file.endswith('.txt'):
abs_path = os.path.join(cur_path,file)
with open(abs_path) as f:
if keyword in f.read():
print('在【%s】这个文件里面有你查找的内容!'%abs_path)

else:
print('你传入的路径不存在')


猜你喜欢

转载自www.cnblogs.com/kexinwang/p/11105535.html
今日推荐