259 lines
8.5 KiB
Python
259 lines
8.5 KiB
Python
import os
|
||
import shutil
|
||
import re
|
||
import sys
|
||
|
||
|
||
def find_target_folders(root_path, output_path):
|
||
"""
|
||
递归遍历根目录,找到包含特定关键词子文件夹的父文件夹,并进行相应的复制和重命名操作
|
||
"""
|
||
|
||
# 确保输出目录存在
|
||
os.makedirs(os.path.join(output_path, "DCE0"), exist_ok=True)
|
||
os.makedirs(os.path.join(output_path, "DCE1"), exist_ok=True)
|
||
|
||
# 使用递归函数遍历
|
||
for item in os.listdir(root_path):
|
||
item_path = os.path.join(root_path, item)
|
||
if os.path.isdir(item_path):
|
||
process_directory(item_path, output_path)
|
||
|
||
|
||
def process_directory(current_path, output_path):
|
||
"""
|
||
递归处理目录
|
||
"""
|
||
# 获取当前文件夹的名称
|
||
current_folder_name = os.path.basename(current_path)
|
||
|
||
# 检查当前目录下是否包含目标文件夹
|
||
contains_disco = False
|
||
contains_twist = False
|
||
|
||
for sub_item in os.listdir(current_path):
|
||
sub_item_path = os.path.join(current_path, sub_item)
|
||
if os.path.isdir(sub_item_path):
|
||
# 检查是否包含DISCO目标文件夹
|
||
if "DISCO Dyn Mph 4 Nav" in sub_item:
|
||
contains_disco = True
|
||
|
||
# 检查是否包含twist目标文件夹
|
||
if "twist_dixon_tra_dyn_p4" in sub_item:
|
||
contains_twist = True
|
||
|
||
# 如果包含DISCO目标文件夹
|
||
if contains_disco:
|
||
print(f"找到包含DISCO文件夹的父文件夹: {current_folder_name} (路径: {current_path})")
|
||
process_disco_parent_folder(current_path, current_folder_name, output_path)
|
||
# 找到后不需要继续递归,因为这个父文件夹就是我们需要的
|
||
return
|
||
|
||
# 如果包含twist目标文件夹
|
||
if contains_twist:
|
||
print(f"找到包含twist文件夹的父文件夹: {current_folder_name} (路径: {current_path})")
|
||
process_twist_parent_folder(current_path, current_folder_name, output_path)
|
||
# 找到后不需要继续递归,因为这个父文件夹就是我们需要的
|
||
return
|
||
|
||
# 如果不包含目标文件夹,继续递归遍历子目录
|
||
for sub_item in os.listdir(current_path):
|
||
sub_item_path = os.path.join(current_path, sub_item)
|
||
if os.path.isdir(sub_item_path):
|
||
process_directory(sub_item_path, output_path)
|
||
|
||
|
||
def process_disco_parent_folder(folder_path, folder_name, output_path):
|
||
"""
|
||
处理包含DISCO目标文件夹的父文件夹
|
||
"""
|
||
|
||
# 遍历父文件夹内的所有子文件夹
|
||
for sub_item in os.listdir(folder_path):
|
||
sub_item_path = os.path.join(folder_path, sub_item)
|
||
|
||
if not os.path.isdir(sub_item_path):
|
||
continue
|
||
|
||
# 查找包含 "DISCO Dyn Mph 4 Nav_0" 的文件夹
|
||
if "DISCO Dyn Mph 4 Nav_0" in sub_item:
|
||
target_path = os.path.join(output_path, "DCE0", f"DCE0_{folder_name}")
|
||
|
||
# 如果目标路径已存在,先删除
|
||
if os.path.exists(target_path):
|
||
shutil.rmtree(target_path)
|
||
|
||
# 复制文件夹
|
||
try:
|
||
shutil.copytree(sub_item_path, target_path)
|
||
print(f" 复制到 DCE0: {sub_item} -> DCE0_{folder_name}")
|
||
except Exception as e:
|
||
print(f" 复制失败: {e}")
|
||
|
||
# 查找包含 "DISCO Dyn Mph 4 Nav_7" 的文件夹
|
||
elif "DISCO Dyn Mph 4 Nav_7" in sub_item:
|
||
target_path = os.path.join(output_path, "DCE1", f"DCE1_{folder_name}")
|
||
|
||
# 如果目标路径已存在,先删除
|
||
if os.path.exists(target_path):
|
||
shutil.rmtree(target_path)
|
||
|
||
# 复制文件夹
|
||
try:
|
||
shutil.copytree(sub_item_path, target_path)
|
||
print(f" 复制到 DCE1: {sub_item} -> DCE1_{folder_name}")
|
||
except Exception as e:
|
||
print(f" 复制失败: {e}")
|
||
|
||
|
||
def process_twist_parent_folder(folder_path, folder_name, output_path):
|
||
"""
|
||
处理包含twist目标文件夹的父文件夹
|
||
"""
|
||
|
||
# 收集所有符合条件的文件夹
|
||
candidate_folders = []
|
||
|
||
# # 首先找到包含"twist_dixon_tra_dyn_p4"的文件夹
|
||
# twist_folder_path = None
|
||
# for sub_item in os.listdir(folder_path):
|
||
# sub_item_path = os.path.join(folder_path, sub_item)
|
||
#
|
||
# if not os.path.isdir(sub_item_path):
|
||
# continue
|
||
#
|
||
# # 找到包含"twist_dixon_tra_dyn_p4"的文件夹
|
||
# if "twist_dixon_tra_dyn_p4" in sub_item:
|
||
# twist_folder_path = sub_item_path
|
||
# break
|
||
#
|
||
# if not twist_folder_path:
|
||
# print(f" 错误: 未找到包含twist_dixon_tra_dyn_p4的文件夹")
|
||
# return
|
||
|
||
# 遍历twist文件夹内的子文件夹
|
||
for sub_item in os.listdir(folder_path):
|
||
sub_item_path = os.path.join(folder_path, sub_item)
|
||
|
||
if not os.path.isdir(sub_item_path):
|
||
continue
|
||
|
||
# 检查是否包含目标关键词且以"_W"结尾
|
||
if "vibe-twist_dixon_tra_dyn_p4_TTC" in sub_item and sub_item.endswith("_W"):
|
||
# 提取TTC值
|
||
match = re.search(r'TTC=([\d\.]+)s_', sub_item)
|
||
if match:
|
||
ttc_value = float(match.group(1))
|
||
candidate_folders.append((sub_item, sub_item_path, ttc_value))
|
||
print(f" 找到候选文件夹: {sub_item}, TTC={ttc_value}")
|
||
|
||
if not candidate_folders:
|
||
print(f" 未找到符合条件的文件夹")
|
||
return
|
||
|
||
# 按TTC值排序
|
||
candidate_folders.sort(key=lambda x: x[2])
|
||
|
||
# 找出TTC值最小的文件夹(用于DCE0)
|
||
dce0_candidate = candidate_folders[0]
|
||
target_path = os.path.join(output_path, "DCE0", f"DCE0_{folder_name}")
|
||
|
||
# 如果目标路径已存在,先删除
|
||
if os.path.exists(target_path):
|
||
shutil.rmtree(target_path)
|
||
|
||
# 复制文件夹到DCE0
|
||
try:
|
||
shutil.copytree(dce0_candidate[1], target_path)
|
||
print(f" 复制到 DCE0: {dce0_candidate[0]} -> DCE0_{folder_name}")
|
||
except Exception as e:
|
||
print(f" 复制失败: {e}")
|
||
|
||
# 找出TTC值大于200且最小的文件夹(用于DCE1)
|
||
dce1_candidate = None
|
||
for item, item_path, ttc_value in candidate_folders:
|
||
if ttc_value > 200:
|
||
dce1_candidate = (item, item_path, ttc_value)
|
||
break
|
||
|
||
if dce1_candidate:
|
||
target_path = os.path.join(output_path, "DCE1", f"DCE1_{folder_name}")
|
||
|
||
# 如果目标路径已存在,先删除
|
||
if os.path.exists(target_path):
|
||
shutil.rmtree(target_path)
|
||
|
||
# 复制文件夹到DCE1
|
||
try:
|
||
shutil.copytree(dce1_candidate[1], target_path)
|
||
print(f" 复制到 DCE1: {dce1_candidate[0]} -> DCE1_{folder_name}")
|
||
except Exception as e:
|
||
print(f" 复制失败: {e}")
|
||
else:
|
||
print(f" 未找到TTC>200的文件夹")
|
||
|
||
|
||
def main():
|
||
"""
|
||
主函数
|
||
"""
|
||
# 获取用户输入
|
||
print("文件夹处理工具")
|
||
print("=" * 50)
|
||
|
||
# # 输入根目录路径
|
||
# while True:
|
||
# root_path = input("请输入要遍历的根目录路径: ").strip()
|
||
# if os.path.isdir(root_path):
|
||
# break
|
||
# else:
|
||
# print(f"错误: 路径 '{root_path}' 不存在或不是目录")
|
||
#
|
||
# # 输入输出路径
|
||
# while True:
|
||
# output_path = input("请输入输出目录路径: ").strip()
|
||
# # 尝试创建输出目录
|
||
# try:
|
||
# os.makedirs(output_path, exist_ok=True)
|
||
# break
|
||
# except Exception as e:
|
||
# print(f"错误: 无法创建输出目录 '{output_path}': {e}")
|
||
|
||
root_path = "E:\\2pi(增强已分期)"
|
||
output_path = "F:\\pi2_convert"
|
||
|
||
# 执行处理
|
||
print("\n开始处理...")
|
||
print("-" * 50)
|
||
|
||
try:
|
||
find_target_folders(root_path, output_path)
|
||
print("\n处理完成!")
|
||
|
||
# 显示结果
|
||
dce0_path = os.path.join(output_path, "DCE0")
|
||
dce1_path = os.path.join(output_path, "DCE1")
|
||
|
||
if os.path.exists(dce0_path):
|
||
dce0_items = os.listdir(dce0_path)
|
||
print(f"\nDCE0文件夹中有 {len(dce0_items)} 个文件夹:")
|
||
for item in dce0_items:
|
||
print(f" {item}")
|
||
|
||
if os.path.exists(dce1_path):
|
||
dce1_items = os.listdir(dce1_path)
|
||
print(f"\nDCE1文件夹中有 {len(dce1_items)} 个文件夹:")
|
||
for item in dce1_items:
|
||
print(f" {item}")
|
||
|
||
except Exception as e:
|
||
print(f"处理过程中出现错误: {e}")
|
||
import traceback
|
||
traceback.print_exc()
|
||
return 1
|
||
|
||
return 0
|
||
|
||
|
||
if __name__ == "__main__":
|
||
sys.exit(main()) |