Python遍历指定路径下的某个文件类型,并在其查找所有符合要求的正则表达式

遍历指定路径下的某个文件类型(遍历的文件类型可自定),并在其中每个文件中查找所有符合个人要求的正则表达式

点击打开链接 python资料汇总

#指定目录下指定文件类型查找指定正则数据
#write by 执笔画红颜
import os,re,sys

print('please input the path')
Searchpath=input()
list=os.listdir(Searchpath)
os.chdir(Searchpath)
print('请输入文件类型,如 \'.txt\'')
type=input()
print(list)
for filename in list:
	#print(filename)
	Allsuffix=os.path.splitext(filename)
	file,suffix=Allsuffix
	#print(suffix)
	if suffix==str(type):
		print(filename)
		file=open(filename)
		data=file.read()
		print(data)
		
		print('**********input the re rule*********')
		ReRule=input()
		regex=re.compile(r''+ReRule+'')
		redata=regex.findall(data)
		
		print(redata)
		print('----------------------------')
		file.close()


你可能感兴趣的:(Python)