| import subprocess |
| import sys |
| import os |
| import shutil |
| import time |
|
|
| def main(): |
| |
| current_dir = os.path.dirname(os.path.abspath(__file__)) |
| getdata_script = os.path.join(current_dir, "getdata.py") |
| gendata_script = os.path.join(current_dir, "gendatav2.py") |
| |
| |
| chapters = [ |
| "新刻金瓶梅詞話卷之十_第九十一回", |
| "新刻金瓶梅詞話卷之十_第九十二回", |
| "新刻金瓶梅詞話卷之十_第九十三回", |
| "新刻金瓶梅詞話卷之十_第九十四回", |
| "新刻金瓶梅詞話卷之十_第九十五回", |
| "新刻金瓶梅詞話卷之十_第九十六回", |
| "新刻金瓶梅詞話卷之十_第九十七回", |
| "新刻金瓶梅詞話卷之十_第九十八回", |
| "新刻金瓶梅詞話卷之十_第九十九回", |
| "新刻金瓶梅詞話卷之十_第一百回" |
| ] |
| |
| for i, chapter_name in enumerate(chapters): |
| print(f"\n=====================================") |
| print(f"开始处理章节: {chapter_name}") |
| print(f"=====================================") |
|
|
| if os.path.exists(chapter_name): |
| shutil.rmtree(chapter_name) |
| |
| command = [ |
| sys.executable, |
| getdata_script, |
| "--chapter", |
| chapter_name |
| ] |
| |
| print(f"正在执行命令: {' '.join(command)}") |
| |
| |
| try: |
| subprocess.run(command, check=True) |
| print(f"{chapter_name} 的 getdata.py 执行成功") |
| |
| |
| raw_dir = os.path.join(current_dir, "out", "raw") |
| if os.path.exists(raw_dir): |
| for filename in os.listdir(raw_dir): |
| if filename.startswith("0000_paragraphs") or filename.startswith("0001_paragraphs") or filename.startswith("0002_paragraphs") or filename.startswith("0000_pages") or filename.startswith("0001_pages") or filename.startswith("0002_pages"): |
| file_to_delete = os.path.join(raw_dir, filename) |
| try: |
| os.remove(file_to_delete) |
| print(f"已删除: {file_to_delete}") |
| except Exception as e: |
| print(f"删除 {file_to_delete} 失败: {e}") |
| |
| |
| print(f"正在运行 {chapter_name} 的 gendatav2.py") |
| gendata_command = [sys.executable, gendata_script] |
| subprocess.run(gendata_command, check=True) |
| print(f"{chapter_name} 的 gendatav2.py 执行成功") |
| |
| |
| target_dir = os.path.join(current_dir, chapter_name) |
| os.makedirs(target_dir, exist_ok=True) |
| print(f"确保目标文件夹存在: {target_dir}") |
| |
| |
| for folder_name in ["out", "out2"]: |
| src_dir = os.path.join(current_dir, folder_name) |
| dest_dir = os.path.join(target_dir, folder_name) |
| |
| if os.path.exists(src_dir): |
| |
| if os.path.exists(dest_dir): |
| shutil.rmtree(dest_dir) |
| shutil.move(src_dir, dest_dir) |
| print(f"已将 {folder_name} 移动到 {chapter_name} 内") |
| else: |
| print(f"未找到 {folder_name} 文件夹,跳过") |
| |
| except subprocess.CalledProcessError as e: |
| print(f"{chapter_name} 的 getdata.py 执行失败,返回码: {e.returncode}") |
| except Exception as e: |
| print(f"发生错误: {e}") |
| |
| |
| if i < len(chapters) - 1: |
| print(f"\n{chapter_name} 处理完成。") |
| print("等待 3 分钟 (180秒) 后继续处理下一个章节...") |
| for remaining in range(180, 0, -10): |
| print(f"还剩 {remaining} 秒...") |
| time.sleep(min(10, remaining)) |
| print("延迟结束,准备继续。\n") |
|
|
| print("\n=====================================") |
| print("所有章节处理完成,准备将生成的章节文件夹移动到上一层目录...") |
| print("=====================================") |
| |
| parent_dir = os.path.dirname(current_dir) |
| for chapter_name in chapters: |
| src_dir = os.path.join(current_dir, chapter_name) |
| dest_dir = os.path.join(parent_dir, chapter_name) |
| |
| if os.path.exists(src_dir): |
| try: |
| |
| if os.path.exists(dest_dir): |
| print(f"上一层目录已存在 {chapter_name},正在删除旧文件夹...") |
| shutil.rmtree(dest_dir) |
| shutil.move(src_dir, dest_dir) |
| print(f"成功将 {chapter_name} 移动到: {parent_dir}") |
| except Exception as e: |
| print(f"移动 {chapter_name} 时发生错误: {e}") |
| else: |
| print(f"当前目录未找到 {chapter_name} 文件夹,跳过。") |
| |
| print("\n所有任务执行完毕!") |
|
|
| if __name__ == "__main__": |
| main() |
|
|