《可爱的Python》读书笔记(四)
# -*- coding: utf-8 -*-import os
def cdcGrep(cdcpath, keyword):
filelist = os.listdir(cdcpath) # 搜索目录中的文件
cdcpath = cdcpath + "\\"
for cdc in filelist: # 循环文件列表
if os.path.isdir(cdcpath+cdc):
print('搜索子目录{}'.format(cdcpath+cdc))
cdcGrep(cdcpath+cdc, keyword)# 若是子目录,则递归调用完成查找
else:
if cdc.endswith('.cdc'):
print('找到目标文件:{}'.format(cdc))
cdcfile = open(cdcpath + cdc) # 拼合文件路径,并打开文件
for line in cdcfile.readlines(): # 读取文件每一行,并循坏
if keyword in line: # 判断是否有关键词在行中
print(line)
cdcGrep('F:\\back\\', 'images')
页:
[1]