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

技术问询:如何移除数据集中的父文件夹?附目录调整示例

批量移除数据集目录中的父文件夹层级

下面提供几种实用方法帮你完成需求,把类似images256/w/waiting_room/*的路径改成images256/waiting_room/*:

方法一:终端命令(Linux/macOS)

直接用find配合mv批量移动子目录,再清理空文件夹:

  1. 移动二级子目录到目标层级
find images256 -mindepth 2 -type d -exec mv {} images256/ \;
  1. 删除空的中间父文件夹
find images256 -mindepth 1 -type d -empty -delete

方法二:PowerShell命令(Windows)

Windows环境下用PowerShell执行:

  1. 移动二级子目录
Get-ChildItem -Path .\images256\ -Directory -Recurse -Depth 1 | ForEach-Object {
    Move-Item -Path $_.FullName -Destination .\images256\
}
  1. 清理空文件夹
Get-ChildItem -Path .\images256\ -Directory -Recurse | Where-Object { $_.GetFiles().Count -eq 0 -and $_.GetDirectories().Count -eq 0 } | Remove-Item

方法三:Python脚本(跨平台)

如果需要跨平台适配,或者要处理目录重复的情况(比如不同父文件夹下有同名子目录),用Python脚本更灵活:

import os
import shutil

root_dir = "images256"

for parent_dir in os.listdir(root_dir):
    parent_path = os.path.join(root_dir, parent_dir)
    if not os.path.isdir(parent_path):
        continue
    # 遍历父文件夹下的所有子目录
    for sub_dir in os.listdir(parent_path):
        sub_path = os.path.join(parent_path, sub_dir)
        if not os.path.isdir(sub_path):
            continue
        target_path = os.path.join(root_dir, sub_dir)
        # 如果目标目录已存在,合并内部文件
        if os.path.exists(target_path):
            for item in os.listdir(sub_path):
                shutil.move(os.path.join(sub_path, item), os.path.join(target_path, item))
        else:
            shutil.move(sub_path, target_path)
    # 删除空的父文件夹
    if not os.listdir(parent_path):
        os.rmdir(parent_path)

把脚本放在数据集根目录运行即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 04:40:23