# -*- coding: utf-8 -*-
import os
#查找路径
PATH='D:\\lcc'
#查找文件名后缀,如果为''则查找所有文件
SUFFIX = '.c' #查找.c文件
#查找的字符串
STRING = '06'
#存放文件名的链表
files = []
result = []
def getFiles(path):
#如果路径不是以\\结尾则补上
if path[-1:] != '\\':
path += '\\'
dirFiles = os.listdir(path)
for file in dirFiles:
filePath = path + file
if os.path.isdir(filePath):
getFiles(filePath + '\\')
else:
if SUFFIX == '' or file[-2:] == SUFFIX:
files.append(filePath)
def findStr(files, str):
for file in files:
fp = open(file, 'r')
lines = fp.readlines()
cnt = 0
for line in lines:
cnt+=1
if line.find(str) > -1:
#print file, ':', cnt , line
tmp = (file, cnt, line)
result.append(tmp)
fp.close()
if __name__ == '__main__':
getFiles(PATH)
#print 'files: ',files
findStr(files, STRING)
print result