python 获取指定目录下的图片文件

import  os

#获取指定路径下所有的图片文件
def listfile(dirpath):
    # pathdir = os.listdir(dirpath)   #图片列表
    imgFileList = os.listdir(dirpath)   #图片列表
    absPath = os.path.realpath(__file__)  # 获取当前执行脚本的绝对路径
    dirPath = os.path.dirname(absPath)  # 去掉文件名,返回目录
    
    for filename in imgFileList:
        filepath = os.path.join(dirpath, filename)  #图片的绝对路径
        if os.path.isdir(filepath):
            listfile(filepath)
            print(filepath)
        else:
            if os.path.isfile(filepath) and filename.lower().endswith('.jpg'):
                print(os.path.join(dirPath, filename))
                
dirpath=r'C:\Users\pic'
listfile(dirpath)

猜你喜欢

转载自blog.csdn.net/weixin_38819889/article/details/88566757