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 with a specified extension in a given directory and all its subdirectories.
Technology Stack : os, re
Code Type : Function
Code Difficulty : Intermediate
import os
import re
def find_files(directory, extension):
"""
查找指定目录及其子目录下所有指定扩展名的文件。
:param directory: 要搜索的目录
:param extension: 文件扩展名
:return: 找到的文件列表
"""
files = []
for root, dirs, filenames in os.walk(directory):
for filename in filenames:
if filename.endswith(extension):
files.append(os.path.join(root, filename))
return files