def get_content(f): try: with open(f) as ff: buf = ff.read(-1) except Exception,e: buf = '' usable.log("打开文件失败 %s" % (f,), level=usable.ERROR)
def find_content(dir, c, file_name_pattern = '*.*', is_recursive = False): fs = find_files(dir, file_name_pattern, is_recursive) usable.log("查询结果") for f in fs: buf = get_content(f) if buf: #文本内容,需要进行编码转换 rs = find_content_in_buffer(c, buf) if rs: #查询结果,文件名称和行数 usable.log("文件名称:%s 行号:%s" % (f, str(rs)))