You can download this code by clicking the button below.
This code is now available for download.
This function searches for all files with a specific extension in a given directory and returns a list of full paths to these files.
Technology Stack : os, re
Code Type : File search
Code Difficulty : Beginner
import os
import re
def find_files(directory, extension):
"""
查找指定目录下所有具有指定扩展名的文件。
:param directory: 要搜索的目录路径
:param extension: 要查找的文件扩展名
:return: 包含所有找到的文件的列表
"""
matches = []
for root, _, files in os.walk(directory):
for filename in files:
if filename.endswith(extension):
matches.append(os.path.join(root, filename))
return matches