root_files = [e for e in TARGET_DIR.iterdir() if e.is_file() and not e.name.startswith(“~$”)]size_hash = defaultdict(list)for f in root_files:try: size_hash[(f.stat().st_size, file_hash(f))].append(f)except:pass
清理重复文件
dups = {k:v for k,v in size_hash.items() if len(v)>1}to_del=set()for g in dups.values():keep=min(g,key=lambda p:len(p.name))[to_del.add(d) for d in g if d!=keep]
文件分类移动
moves=[]for f in root_files:if f in to_del:continued = TARGET_DIR / f.suffix.lower().lstrip(‘.’) or “no_ext”nn=normalize_name(f.name); dst,i=d/nn,1while dst.exists() and dst!=f:s,e=os.path.splitext(nn); dst=d/f”{s}_{i}{e}”;i+=1moves.append((f,dst))
输出日志
print(f”总数:{len(root_files)}|待删除重复:{len(to_del)}|待分类:{len(moves)}”)for s,d in moves:print(f”{s.name} → {d.parent.name}/{d.name}”)for d in to_del:print(f”✕ 清理重复:{d.name}”)if not DRY_RUN:[d.parent.mkdir(parents=True,exist_ok=True) for _,d in moves][shutil.move(str(s),str(d)) for s,d in moves][d.unlink(missing_ok=True) for d in to_del]print(“🎉 文件夹整理完成!”)else:print(“⚠️ 预览模式,修改DRY_RUN=False即可执行”)1、文件→另存为,按以下配置保存:✅文件名:organize.py|✅保存类型:所有文件|✅编码:UTF-8|✅保存到目标下载文件夹