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

如何用Python批量移动多组scope文件夹内容到对应目标路径

批量迁移scope目录内容实现脚本

前置说明

  • 脚本仅依赖Python内置的os、shutil模块,无需额外安装第三方库
  • 运行前请先备份源目录数据,避免误操作导致文件丢失
  • 若目标路径存在同名文件,脚本默认覆盖,如有去重/保留需求可自行调整判断逻辑
  • 可先将移动语句替换为打印语句验证路径匹配规则,确认无误后再执行真实移动操作

完整代码

import os
import shutil

# 配置源根目录和目标根目录
SOURCE_ROOT = "test/subtest/"
TARGET_ROOT = "newfold/sebtest/"

# 遍历源根目录下的所有条目
for entry in os.listdir(SOURCE_ROOT):
    entry_path = os.path.join(SOURCE_ROOT, entry)
    # 仅处理名称为纯数字的目录
    if os.path.isdir(entry_path) and entry.isdigit():
        # 拼接当前编号对应的源scope路径
        source_scope = os.path.join(entry_path, "scope")
        if not os.path.exists(source_scope):
            continue
        # 拼接对应的目标scope路径
        target_scope = os.path.join(TARGET_ROOT, entry, "scope")
        # 自动创建不存在的目标目录
        os.makedirs(target_scope, exist_ok=True)
        # 移动源scope下的所有内容到目标scope
        for item in os.listdir(source_scope):
            source_item = os.path.join(source_scope, item)
            target_item = os.path.join(target_scope, item)
            shutil.move(source_item, target_item)
        # 可选:移动完成后删除空的源scope目录
        # if len(os.listdir(source_scope)) == 0:
        #     os.rmdir(source_scope)

验证方法

你可以先将shutil.move(source_item, target_item)替换为print(f"移动:{source_item} -> {target_item}"),运行后查看打印的路径是否符合预期,确认规则正确后再换回移动语句执行即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 05:27:00