获取自己本地项目的所有图片

执行对应脚本文件


截屏2023-07-27 10.05.52.png
import os

def get_all_images(project_path, output_folder):
    for root, dirs, files in os.walk(project_path):
        for file in files:
            if file.endswith(".png") or file.endswith(".jpg") or file.endswith(".jpeg"):
                file_path = os.path.join(root, file)
                output_path = os.path.join(output_folder, file)
                os.makedirs(os.path.dirname(output_path), exist_ok=True)
                os.rename(file_path, output_path)

project_path = "/Users/saman0/Desktop/main/PZSwift/PZSwift/Assets.xcassets"
output_folder = "/Users/saman0/Desktop/temp"
get_all_images(project_path, output_folder)

但是上面那个脚本执行会把之前文件夹中的图片也跟着删除
下面这个就是复制图片到新文件夹中

import os
import shutil

def copy_all_images(project_path, output_folder):
    for root, dirs, files in os.walk(project_path):
        for file in files:
            if file.endswith(".png") or file.endswith(".jpg") or file.endswith(".jpeg"):
                file_path = os.path.join(root, file)
                output_path = os.path.join(output_folder, file)
                os.makedirs(os.path.dirname(output_path), exist_ok=True)
                shutil.copy(file_path, output_path)

project_path = "/Users/saman0/Desktop/main/PZSwift/PZSwift/Assets.xcassets"
output_folder = "/Users/saman0/Desktop/haha"
copy_all_images(project_path, output_folder)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容