图片重命名

import os
import shutil

# 定义大文件夹A和B的路径
folder_a_path = "C:\\Users\XUB\Desktop\\benign"
folder_b_path = "C:\\Users\XUB\Desktop\\bb"

# 获取大文件夹A中的子文件夹列表
subfolders = [f for f in os.listdir(folder_a_path) if os.path.isdir(os.path.join(folder_a_path, f))]

# 遍历每个子文件夹
for subfolder in subfolders:
    subfolder_path = os.path.join(folder_a_path, subfolder)

    # 获取子文件夹中的图片文件列表
    image_files = [f for f in os.listdir(subfolder_path) if f.lower().endswith(('.jpg', '.png', '.jpeg'))]

    # 遍历每个图片文件并移动到大文件夹B中的对应子文件夹下
    for image_file in image_files:
        new_filename = subfolder + image_file
        new_file_path = os.path.join(folder_b_path, subfolder, new_filename)
        old_file_path = os.path.join(subfolder_path, image_file)

        # 创建目标子文件夹(如果不存在)
        os.makedirs(os.path.dirname(new_file_path), exist_ok=True)

        # 移动图片文件
        shutil.move(old_file_path, new_file_path)

print("操作完成")

你可能感兴趣的:(工具代码,python)