You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python脚本让test_features文件夹镜像test_cases的嵌套结构?

实现test_features目录与test_cases目录结构镜像的Python脚本

需求说明

需要让test_features目录完全镜像test_cases的文件结构:

  • 为test_cases下每个test.py文件,在test_features对应路径创建同名的test.feature文件(若不存在)
  • 删除test_features中所有test_cases里没有对应的文件和目录,包括空目录

完整脚本代码

import os
import shutil

def sync_test_directories(source_dir, target_dir):
    # 确保源目录存在
    if not os.path.exists(source_dir):
        print(f"源目录 {source_dir} 不存在,终止操作")
        return

    # 第一步:遍历源目录,同步创建目标目录结构和feature文件
    for root, dirs, files in os.walk(source_dir):
        # 计算当前目录在目标中的对应路径
        relative_path = os.path.relpath(root, source_dir)
        target_root = os.path.join(target_dir, relative_path)

        # 创建目标目录(如果不存在)
        if not os.path.exists(target_root):
            os.makedirs(target_root)
            print(f"创建目录: {target_root}")

        # 处理每个test.py文件,创建对应的feature文件
        for file in files:
            if file.endswith(".py") and file.startswith("test"):
                source_file = os.path.join(root, file)
                # 替换后缀为.feature
                target_file_name = file.replace(".py", ".feature")
                target_file = os.path.join(target_root, target_file_name)

                if not os.path.exists(target_file):
                    # 创建空的feature文件
                    with open(target_file, 'w', encoding='utf-8') as f:
                        pass
                    print(f"创建特性文件: {target_file}")

    # 第二步:遍历目标目录,删除冗余文件和目录
    for root, dirs, files in os.walk(target_dir, topdown=False):
        # 计算当前目录在源中的对应路径
        relative_path = os.path.relpath(root, target_dir)
        source_root = os.path.join(source_dir, relative_path)

        # 删除目标中不存在于源的文件
        for file in files:
            if file.endswith(".feature"):
                # 转换为源目录对应的py文件名
                source_file_name = file.replace(".feature", ".py")
                source_file = os.path.join(source_root, source_file_name)

                if not os.path.exists(source_file):
                    target_file = os.path.join(root, file)
                    os.remove(target_file)
                    print(f"删除冗余特性文件: {target_file}")

        # 删除目标中不存在于源的目录,以及空目录
        if not os.path.exists(source_root) or len(os.listdir(root)) == 0:
            try:
                os.rmdir(root)
                print(f"删除冗余/空目录: {root}")
            except OSError:
                # 目录不为空(可能还有非feature文件),跳过
                pass

if __name__ == "__main__":
    # 定义源和目标目录路径
    TEST_CASES_DIR = "test_cases"
    TEST_FEATURES_DIR = "test_features"

    sync_test_directories(TEST_CASES_DIR, TEST_FEATURES_DIR)
    print("目录同步完成")

脚本说明

  • 同步创建结构:使用os.walk遍历源目录,按相对路径创建目标目录,对每个test.py生成对应的空test.feature文件
  • 删除冗余内容:反向遍历目标目录(从最深层开始),检查每个feature文件是否有对应的py文件,没有则删除;检查目录是否在源中存在或为空,是则删除
  • 异常处理:跳过删除非空目录的情况,避免误删无关文件;先检查源目录是否存在,防止无效操作

使用方法

  1. 将脚本放在两个目录的同级目录下
  2. 直接运行脚本即可自动完成同步操作

内容的提问来源于stack exchange,提问作者John Doe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 07:57:23