美文网首页
获取自己本地项目的所有图片

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

作者: saman0 | 来源:发表于2023-07-26 11:09 被阅读0次

执行对应脚本文件


截屏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)

相关文章

网友评论

      本文标题:获取自己本地项目的所有图片

      本文链接:https://www.haomeiwen.com/subject/wflkpdtx.html