import os import shutil from pathlib import Path def process_png_files(target_path): """ 在指定路径中查找所有PNG图片,为每个PNG文件创建同名文件夹,并将图片复制到文件夹中 """ # 将路径转换为Path对象 path = Path(target_path) # 检查路径是否存在 if not path.exists(): print(f"错误:路径 '{target_path}' 不存在") return # 查找所有的PNG文件(不区分大小写) png_files = list(path.glob("*.png")) + list(path.glob("*.PNG")) if not png_files: print(f"在路径 '{target_path}' 中没有找到PNG文件") return print(f"找到 {len(png_files)} 个PNG文件") # 处理每个PNG文件 for png_file in png_files: try: # 获取文件名(不含扩展名) file_stem = png_file.stem # 创建同名文件夹 folder_path = path / file_stem folder_path.mkdir(exist_ok=True) # 目标文件路径 destination = folder_path / png_file.name # 复制文件 shutil.copy2(png_file, destination) print(f"✓ 已处理: {png_file.name} -> {folder_path.name}/") except Exception as e: print(f"✗ 处理文件 {png_file.name} 时出错: {e}") if __name__ == "__main__": # 请修改为你实际的路径 target_path = r"../example/src/main/assets/pic" # 修改为你的实际路径 print(f"开始处理路径: {target_path}") process_png_files(target_path) print("处理完成!")