python快速查找文件
时间: 2023-09-09 08:10:24 浏览: 95
你可以使用os模块中的walk函数遍历文件夹中的所有文件,并使用字符串操作函数判断文件名是否符合要求,如下所示:
import os
def search_files(folder_path, extension):
result = []
for root, dirs, files in os.walk(folder_path):
for file in files:
if file.endswith(extension):
result.append(os.path.join(root, file))
return result
你可以使用该函数查找指定文件夹内指定扩展名的文件,例如:
search_files('/path/to/folder', '.txt')
该函数将返回一个包含所有符合条件的文件路径的列表。
相关推荐

















