You can download this code by clicking the button below.
This code is now available for download.
This function is used to find all file paths that match the specified regular expression in the given directory.
Technology Stack : os, re
Code Type : Function
Code Difficulty : Intermediate
import os
import re
def find_files(root_dir, pattern):
"""
查找匹配指定模式的文件路径。
:param root_dir: 根目录路径
:param pattern: 文件名模式,使用正则表达式
:return: 匹配的文件路径列表
"""
matches = []
for dirpath, dirnames, filenames in os.walk(root_dir):
for filename in filenames:
if re.match(pattern, filename):
matches.append(os.path.join(dirpath, filename))
return matches