python复制一个文件下下所有图片(包括子文件下的)到指定目录

参考博客:https://blog.csdn.net/qianbin3200896/article/details/107763861/

参考博客:https://www.cnblogs.com/xuchen163/p/12781214.html

from glob import glob
import os, random
from shutil import copyfile
source_path=r'F:/workshop/chineseocr-app/train/data/text-gfq/test/33020319540208322'
target_path=r'F:/workshop/chineseocr-app/train/data/text-gfq/test'


def getFileList(dir,Filelist):
    """
    获取文件夹及其子文件夹中文件列表
    输入 dir:文件夹根目录
    输入 ext: 扩展名
    返回: 文件路径列表
    """
    newDir = dir
    if os.path.isfile(dir):
        if dir.endswith(".jpg") or dir.endswith(".JPG") or dir.endswith(".png") or dir.endswith(".PNG"):#图片格式,自己设置
            Filelist.append(dir)
    
    elif os.path.isdir(dir):
        for s in os.listdir(dir):
            newDir=os.path.join(dir,s)
            getFileList(newDir, Filelist)
 
    return Filelist

def copyFile(imageList1, target_path):

    #os.path.splitext(),返回元组,为文件名称与文件后缀格式
    for item in imageList1:
            #将随机选中的jpg文件遍历复制到目标文件夹中
        tmp=os.path.basename(item)
        copyfile(item, target_path+'/'+tmp)

    return

imageList = getFileList(source_path,[])
copyFile(imageList, target_path)

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值