|
|
@@ -0,0 +1,39 @@
|
|
|
+import os
|
|
|
+import sys
|
|
|
+import shutil
|
|
|
+
|
|
|
+while True:
|
|
|
+ target_folder_path = input('输入目标文件夹路径:(输入q退出) ')
|
|
|
+ if target_folder_path.lower() == 'q':
|
|
|
+ sys.exit(0)
|
|
|
+ # 获取目标文件夹下的所有子文件夹
|
|
|
+ try:
|
|
|
+ entries = os.listdir(target_folder_path)
|
|
|
+ except FileNotFoundError:
|
|
|
+ print(f"目标文件夹路径 {target_folder_path} 不存在, 重新输入文件路径, 或者输入q退出")
|
|
|
+ continue
|
|
|
+
|
|
|
+ folders = [entry for entry in entries if os.path.isdir(
|
|
|
+ os.path.join(target_folder_path, entry))]
|
|
|
+
|
|
|
+ for folder in folders:
|
|
|
+ folder_path = os.path.join(target_folder_path, folder)
|
|
|
+ # 获取子文件夹中的所有内容
|
|
|
+ sub_entries = os.listdir(folder_path)
|
|
|
+ sub_folders = [entry for entry in sub_entries if os.path.isdir(
|
|
|
+ os.path.join(folder_path, entry))]
|
|
|
+
|
|
|
+ # 如果子文件夹中只有一个文件夹
|
|
|
+ if len(sub_folders) == 1:
|
|
|
+ sub_folder_path = os.path.join(folder_path, sub_folders[0])
|
|
|
+ # 移动子文件夹中的所有图片文件到上一级目录
|
|
|
+ for file in os.listdir(sub_folder_path):
|
|
|
+ if file.endswith(('.png', '.webp', 'gif', 'jpg')):
|
|
|
+ shutil.move(os.path.join(sub_folder_path, file), folder_path)
|
|
|
+ # 删除子文件夹
|
|
|
+ shutil.rmtree(sub_folder_path)
|
|
|
+ print(f"已处理文件夹: {folder_path}")
|
|
|
+ elif not sub_folders: # 如果子文件夹中没有文件夹
|
|
|
+ print(f"文件夹 {folder_path} 中没有文件夹,已跳过")
|
|
|
+ else:
|
|
|
+ print(f"文件夹 {folder_path} 中包含多个文件夹,未进行处理")
|