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 specific extension in a specified directory and its subdirectories, and returns a list containing the paths of these files.
Technology Stack : os, re
Code Type : Function
Code Difficulty : Intermediate
import os
import re
def find_files(directory, extension):
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith(extension):
yield os.path.join(root, file)
def xxx(directory, file_extension):
"""
查找指定目录下所有特定扩展名的文件。
:param directory: 指定目录路径
:param file_extension: 指定文件扩展名
:return: 一个生成器,包含所有匹配的文件路径
"""
return list(find_files(directory, file_extension))