You can download this code by clicking the button below.
This code is now available for download.
This function is used to find all files that match a specified regular expression pattern in a given directory.
Technology Stack : os, re
Code Type : Function
Code Difficulty : Intermediate
import os
import re
def find_files(directory, pattern):
"""
查找目录中匹配模式的文件。
:param directory: 指定目录
:param pattern: 匹配模式
:return: 匹配文件的列表
"""
matches = []
for root, dirs, files in os.walk(directory):
for filename in files:
if re.match(pattern, filename):
matches.append(os.path.join(root, filename))
return matches