画像を上のフォルダーにコピーし、整列したサブフォルダーを削除し、ファイル名のプレフィックスを追加します

import os

import os
import shutil

root_dir = 'xxxx'  # 文件夹根目录

for subdir, dirs, files in os.walk(root_dir):
    for dir_name in dirs:
        subdir_path = os.path.join(subdir, dir_name)
        if "aligned" in dir_name:
            parent_folder_path = os.path.dirname(subdir_path)
            new_folder_name = os.path.basename(parent_folder_path)
            for file_name in os.listdir(subdir_path):
                file_path = os.path.join(subdir_path, file_name)
                new_file_path = os.path.join(parent_folder_path, new_folder_name + '_' + file_name)
                shutil.move(file_path, new_file_path)
            os.rmdir(subdir_path)

おすすめ

転載: blog.csdn.net/qq_43663979/article/details/130041421