Python常用代码功能

Python常用代码功能

提取一个文件夹下的所有图片(含子目录)到另一个文件夹下

import os
import cv2
import shutil

def getFileList(dir,Filelist, ext=None):
    """
    获取文件夹及其子文件夹中文件列表
    输入 dir:文件夹根目录
    输入 ext: 扩展名
    返回: 文件路径列表
    """
    newDir = dir
    if os.path.isfile(dir):
        if ext is None:
            Filelist.append(dir)
        else:
            if ext in dir[-3:]:
                Filelist.append(dir)
    
    elif os.path.isdir(dir):
        for s in os.listdir(dir):
            newDir=os.path.join(dir,s)
            getFileList(newDir, Filelist, ext)
 
    return Filelist
 
org_img_folder=r'E:documnetsMachine-Learning-Notes'
new_img_folder=r'E:documnetsMachine-Learning-Note'
# 检索文件
imglist = getFileList(org_img_folder, [], 'jpg')
print('本次执行检索到 '+str(len(imglist))+' 张图像
')
 
for imgpath in imglist:
    imgname= os.path.splitext(os.path.basename(imgpath))[0]
    new_path = os.path.join(new_img_folder,imgname)+'.jpg'
    shutil.copy(imgpath,new_path)
    print(imgpath)
    #img = cv2.imread(imgpath, cv2.IMREAD_COLOR)
    # 对每幅图像执行相关操作
原文地址:https://www.cnblogs.com/lwp-nicol/p/14948692.html