| import shutil | |
| from pathlib import Path | |
| BASE_DIR = Path(__file__).resolve().parent | |
| LOG_FILE = BASE_DIR / "deletions.log" | |
| def unique_target(path: Path) -> Path: | |
| """Return a non-colliding path in BASE_DIR.""" | |
| if not path.exists(): | |
| return path | |
| stem, suffix = path.stem, path.suffix | |
| counter = 1 | |
| while True: | |
| candidate = path.with_name(f"{stem}_{counter}{suffix}") | |
| if not candidate.exists(): | |
| return candidate | |
| counter += 1 | |
| def log_delete(log_handle, item: Path): | |
| rel = item.relative_to(BASE_DIR) | |
| line = f"DELETE: {rel.as_posix()}" | |
| print(line) | |
| log_handle.write(line + "\n") | |
| def move_audios_to_base(): | |
| moved = 0 | |
| # Gather first to avoid modifying tree while iterating it. | |
| font_files = [ | |
| p | |
| for p in BASE_DIR.rglob("*") | |
| if p.is_file() and p.suffix.lower() in {".wav", ".mp3", ".ogg", ".flac"} | |
| ] | |
| for src in font_files: | |
| if src.parent == BASE_DIR: | |
| continue | |
| dst = unique_target(BASE_DIR / src.name) | |
| shutil.move(str(src), str(dst)) | |
| print(f"MOVE: {src.relative_to(BASE_DIR).as_posix()} -> {dst.name}") | |
| moved += 1 | |
| return moved | |
| def cleanup_non_audios(log_handle): | |
| deleted_files = 0 | |
| deleted_dirs = 0 | |
| protected_files = { | |
| BASE_DIR / Path(__file__).name, | |
| LOG_FILE, | |
| } | |
| # Delete files that are not audios | |
| for path in BASE_DIR.rglob("*"): | |
| if not path.is_file(): | |
| continue | |
| if path in protected_files: | |
| continue | |
| if path.suffix.lower() in {".wav", ".mp3", ".ogg", ".flac"}: | |
| continue | |
| path.unlink(missing_ok=True) | |
| log_delete(log_handle, path) | |
| deleted_files += 1 | |
| # Remove directories bottom-up | |
| for path in sorted(BASE_DIR.rglob("*"), key=lambda p: len(p.parts), reverse=True): | |
| if not path.is_dir(): | |
| continue | |
| try: | |
| path.rmdir() # only removes if empty | |
| log_delete(log_handle, path) | |
| deleted_dirs += 1 | |
| except OSError: | |
| # Not empty (contains kept font files) | |
| pass | |
| return deleted_files, deleted_dirs | |
| def main(): | |
| print(f"Base directory: {BASE_DIR}") | |
| moved = move_audios_to_base() | |
| with LOG_FILE.open("w", encoding="utf-8") as log_handle: | |
| deleted_files, deleted_dirs = cleanup_non_audios(log_handle) | |
| print("\nDone.") | |
| print(f"Moved audios: {moved}") | |
| print(f"Deleted files: {deleted_files}") | |
| print(f"Deleted folders: {deleted_dirs}") | |
| print(f"Deletion log: {LOG_FILE}") | |
| if __name__ == "__main__": | |
| main() | |
Xet Storage Details
- Size:
- 2.74 kB
- Xet hash:
- 2231a3a3e937db36ff555c82033f90db81b04be7f03265ce9c8632b94c03808f
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.