Created
July 3, 2025 08:06
-
-
Save paulgessinger/e2ceb7fc9547ffc2a7fb41c5d9ba0e3a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# /// script | |
# dependencies = [ | |
# "typer", | |
# "rich", | |
# ] | |
# /// | |
import typer | |
from typing import Annotated | |
from pathlib import Path | |
import shutil | |
import math | |
from rich.progress import Progress, MofNCompleteColumn, SpinnerColumn | |
def main(output_dir: Path, files: list[Path], pattern: str | None = None): | |
pad = math.floor(math.log10(len(files))) + 1 | |
print(pad) | |
prog = Progress( | |
SpinnerColumn(), *Progress.get_default_columns(), MofNCompleteColumn() | |
) | |
output_dir.mkdir(exist_ok=True) | |
with prog: | |
for i, file in prog.track( | |
enumerate(files), description="Copying", total=len(files) | |
): | |
if pattern is not None: | |
target_name = pattern.format(i) | |
else: | |
target_name = f"{file.stem}_{i:>0{pad}d}{file.suffix}" | |
target_path = output_dir / target_name | |
shutil.copyfile(file, target_path) | |
if __name__ == "__main__": | |
typer.run(main) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment