python递归复制文件夹下的所有图片

# -*- coding:utf-8 -*-
import os
import shutil

def copyFile(sourcePath,savePath):
    image_end = ['.jpg', '.JPG', '.PNG', '.png', '.jpeg', '.JPEG']
    for dir_or_file in os.listdir(sourcePath):
        filePath = os.path.join(sourcePath, dir_or_file)
        if os.path.isfile(filePath):# 判断是否为文件
            if os.path.splitext(os.path.basename(filePath))[1] in image_end:#如果文件是图片,则复制,如果都是同一种图片类型也可以用这句:if os.path.basename(filePath).endswith('.jpg'):
                print('this copied pic name is '+ os.path.basename(filePath))# 拷贝jpg文件到自己想要保存的目录下
                shutil.copyfile(filePath,os.path.join(savePath,os.path.basename(filePath)))
            else:
                continue
        elif os.path.isdir(filePath): # 如果是个dir,则再次调用此函数,传入当前目录,递归处理。
            copyFile(filePath,savePath)
        else:print('not file and dir '+os.path.basename(filePath))


if __name__ == '__main__':
    sourcePath="/123"
    savePath="/567"
    copyFile(sourcePath, savePath)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值