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 a regular expression in a specified root 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 root, dirs, files in os.walk(root_dir):
for file in files:
if re.match(pattern, file):
matches.append(os.path.join(root, file))
return matches