Python 递归查找目录下所有jpg文件

# -*- coding:utf-8 -*-

import os
import shutil
def searchDirFile(rootDir,saveDir):
    for dir_or_file in os.listdir(rootDir):
        filePath = os.path.join(rootDir, dir_or_file)
        # 判断是否为文件
        if os.path.isfile(filePath):
            # 如果是文件再判断是否以.jpg结尾,不是则跳过本次循环
            if os.path.basename(filePath).endswith('.jpg'):
                print('imgBox fileName is '+ os.path.basename(filePath))
                # 拷贝jpg文件到自己想要保存的目录下
                shutil.copyfile(filePath,os.path.join(saveDir,os.path.basename(filePath)))
            else:
                continue
        # 如果是个dir,则再次调用此函数,传入当前目录,递归处理。
        elif os.path.isdir(filePath):
            searchDirFile(filePath,saveDir)
        else:print('not file and dir '+os.path.basename(filePath))

猜你喜欢

转载自blog.csdn.net/jerson_al/article/details/84061503
今日推荐