import os
import shutil
# 挑选出的图片所在文件夹的路径
folder_A = '/Data/file/image_path'
# 要筛选的子文件夹的路径
folder_B = '/Data/fold_path'
# 指定的存储目标路径
destination_path = '/Data/dirsave_path'
# 获取A文件夹中的图片文件名
image_names_A = [file.split('.')[0] for file in os.listdir(folder_A) if os.path.isfile(os.path.join(folder_A, file))]
# 遍历A文件夹中的图片名字
for image_name_A in image_names_A:
# 在B文件夹中查找匹配的文件夹名字
matching_folders = [folder for folder in os.listdir(folder_B) if os.path.isdir(os.path.join(folder_B, folder)) and folder == image_name_A]
# 如果找到匹配的文件夹,复制到指定路径
for matching_folder in matching_folders:
source_folder_path = os.path.join(folder_B, matching_folder)
destination_folder_path = os.path.join(destination_path, matching_folder)
shutil.copytree(source_folder_path, destination_folder_path)
根据图片名字,挑选出对应的相同名字的文件夹
于 2023-10-31 11:04:17 首次发布