python 利用os.walk查询该文件夹目录下所有文件是否含有自己要查找的关键字内容

import requests,os

path=r'C:\Users\Administrator\AppData\Local\Programs\Python\Python35\Lib\site-packages\requests'
for root, dirs, files in os.walk(path, topdown=False):
    for name in files:
        filename=os.path.join(root, name)  #文件具体路径
        with open(filename,'r', encoding='gbk', errors='ignore')as fp:#只读形式打开文件
            if 'urlretrieve' in fp.read():         #查询urlretrieve这字符串在那个文件出现过
                print(filename)                     #打印出这个文件的具体路径

猜你喜欢

转载自blog.csdn.net/qq_38282706/article/details/80255606
今日推荐