情形1: 没有子目录:
def get_img_list(path):
is_image_file = lambda x : any(x.endswith(extension)
for extension in ['.jpg', 'png', 'tif'])
return [os.path.join(path, x) for x in os.listdir(path) if is_image_file(x)]
也可以使用glob方法,:
glob.glob(os.path.join(path, '*.jpg'))
情形2: 有子目录
def file_name(file_dir):
L=[]
is_image_file = lambda x : any(x.endswith(extension) for extension in ['.jpg', 'png', 'tif'])
for root, dirs, files in os.walk(file_dir):
for file in files:
if is_image_file(file):
L.append(os.path.join(root, file))
return L