import glob
# 指定的目录地址
folder_path = '/home/w/Pictures/'
# pathname在这里填写匹配规则,我这里使用的匹配后缀的方式,匹配所有后缀是png的图片,"*"表示匹配任何字符
path_file_number=glob.glob(pathname='{}*.png'.format(folder_path))
print("返回所有符合条件的文件",path_file_number)
print("符合条件文件的数量",len(path_file_number))
运行结果
['/home/w/Pictures/Screenshot from 2021-01-07 15-50-55.png', '/home/w/Pictures/qqq.png', '/home/w/Pictures/Screenshot from 2021-06-24 11-00-58.png', '/home/w/Pictures/Screenshot from 2021-06-09 17-38-19.png', '/home/w/Pictures/Screenshot from 2021-06-08 09-22-44.png', '/home/w/Pictures/消息中间件.png', '/home/w/Pictures/Screenshot from 2021-06-29 15-27-13.png', '/home/w/Pictures/Screenshot from 2021-06-21 18-47-15.png', '/home/w/Pictures/意向锁.png', '/home/w/Pictures/Screenshot from 2021-06-24 16-03-03.png', '/home/w/Pictures/Screenshot from 2021-04-21 15-16-12.png', '/home/w/Pictures/009947.png']
12
Process finished with exit code 0