天宫鹤

自定义Python实用函数-返回指定目录及其子目录和指定文件扩展名的文件清单列表

import os
def get_files(file_path, image_types_set=()): """ 返回指定目录及其子目录下、指定文件扩展名的文件清单列表。 若image_types_set参数为空,则返回图片文件清单列表。 若image_types_set参数为['.*'],则返回所有文件清单列表。 """ filenames_list = [] image_types_list = [ext.lower() for ext in image_types_set] # 确保扩展名小写 if len(image_types_list) == 0: image_types_list = ['.png', '.jpg', '.jpeg', '.bmp', '.webp', 'gif', 'svg'] for root, dirs, _files in os.walk(file_path): for _file in _files: # 筛选文件类型 base, ext = os.path.splitext(_file) # 取得文件名、扩展名 ext = ext.lower() # 文件扩展名小写 if image_types_list[0] == '.*': # 判断入参是否为.* if len(ext) >= 3: filenames_list.append(_file) elif ext in image_types_list: filenames_list.append(_file) # filenames_list.append((_file, base, ext)) return filenames_list

 

posted on 2024-04-10 18:50  GoGrid  阅读(18)  评论(0编辑  收藏  举报

导航