在文件夾下檢索多個文件內容

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)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章