在文件夹下检索多个文件内容

import os
import re


# 遍历指定目录,显示目录下的所有文件名
def eachFile(filepath):
	pathDir = os.listdir(filepath)
	for allDir in pathDir:
		child = os.path.join('%s\%s' % (filepath, allDir))
		if os.path.isfile(child):
			readFile(child)
			print(child.decode('gbk')) 
			continue
		eachFile(child)


# 遍历出结果 返回文件的名字
def readFile(filenames):
	fopen = open(filenames, 'r', encoding="UTF-8")  # r 代表read
	fileread = fopen.read()
	fopen.close()
	t = re.search(r'****', fileread)  #检索内容
	if t:
		print "匹配到的文件是:"+filenames
		arr.append(filenames)


if __name__ == "__main__":
	filenames = ‘'  # refer root dir
	arr = []
	eachFile(filenames)
	for i in arr:
		print(i)

你可能感兴趣的:(数据结构,机器学习)