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.
Technology Stack : os, re
Code Type : Function
Code Difficulty :
import os
import re
def find_files_in_directory(directory, extension):
"""
查找指定目录下所有指定扩展名的文件。
:param directory: 指定目录的路径
:param extension: 指定的文件扩展名,例如 '.txt'
:return: 包含所有匹配文件的列表
"""
matches = []
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith(extension):
matches.append(os.path.join(root, file))
return matches
# JSON output