Datasets:
Delete rename_images.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455#!/usr/bin/env python3"""Rename PNG files in ./images to sequential 3-digit names (001.png, 002.png, ...).
Files are processed in ascending order by current filename. Renaming is collision-safevia a temporary suffix."""from __future__ import annotations
from pathlib import Pathimport uuid
def main() -> None: root = Path(__file__).resolve().parent images_dir = root / "images" if not images_dir.is_dir(): raise SystemExit("images directory not found")
# Collect and sort .png files (case-insensitive extension match) png_files = sorted( [p for p in images_dir.iterdir() if p.is_file() and p.suffix.lower() == ".png"], key=lambda p: p.name, )
if not png_files: print("No .png files found") return
# First pass: rename to unique temp names to avoid collisions temp_map = {} for idx, src in enumerate(png_files, start=1): target_name = f"{idx:03d}.png" if src.name == target_name: temp_map[src] = src # No move needed later continue temp_name = f".{src.stem}.{uuid.uuid4().hex}.tmp" temp_path = images_dir / temp_name src.rename(temp_path) temp_map[src] = temp_path
# Second pass: move temps (or originals) to final names for idx, src in enumerate(png_files, start=1): target = images_dir / f"{idx:03d}.png" current = temp_map[src] if current == target: print(f"[ok] {target.name} unchanged") continue current.rename(target) print(f"[ok] {current.name} -> {target.name}")
print("Done.")
if __name__ == "__main__": main()rename_images.pyDeleted-55