import os import re from pathlib import Path def read_end_indices(filename='a.txt'): """读取 end.txt,返回整数列表""" with open(filename, 'r', encoding='utf-8') as f: content = f.read() return [int(x) for x in re.findall(r'-?\d+', content)] def delete_extra_npz(): root = Path("put_toy_in_cabinet") end_indices = read_end_indices() print(f"读取到 {len(end_indices)} 个 episode 的保留上限: {end_indices}") for ep_idx, max_keep in enumerate(end_indices): ep_dir = root / str(ep_idx) if not ep_dir.exists(): print(f"⚠️ Episode {ep_idx} 不存在,跳过") continue # 获取该目录下所有 .npz 文件 npz_files = list(ep_dir.glob("*.npz")) deleted_count = 0 for npz in npz_files: # 提取文件名中的数字(如 "5.npz" → 5) stem = npz.stem if not stem.isdigit(): print(f"❓ 跳过非标准命名文件: {npz}") continue idx = int(stem) if idx > max_keep: npz.unlink() # 删除文件 deleted_count += 1 print(f"✅ Episode {ep_idx}: 删除了 {deleted_count} 个多余的 .npz 文件") if __name__ == "__main__": delete_extra_npz()