import os
import codecs
import re
def search_all(dir_path):
# os.path.getmtime() 函数是获取文件最后修改时间
# os.path.getctime() 函数是获取文件最后创建时间
# 按最后的修改时间,对文件进行排序
files = sorted(os.listdir(file_path), key=lambda x: os.path.getmtime(os.path.join(file_path, x)))
if len(files) == 0:
print('None')
return
for file in files:
print(f"fileName:{file}")
file_path = dir_path+ '/' + file
contents = codecs.open(file_path , 'r')
for content in contents:
# 正则匹配
matchObj = re.match(r'.*(test search world (.*))$', content, re.M | re.I)
if matchObj:
print(matchObj.group())
print(matchObj.group(1))
print(matchObj.group(2))
return
if __name__ == '__main__':
search_all('testdir')