import csv import os import shutil from pathlib import Path def default_desktop() -> Path: user_profile = Path(os.environ.get("USERPROFILE", str(Path.home()))) return user_profile / "Desktop" def read_csv(csv_path: Path) -> list[dict[str, str]]: if not csv_path.is_file(): raise ValueError(f"Die CSV-Datei existiert nicht: {csv_path}") with csv_path.open("r", newline="", encoding="latin-1") as csv_file: return list(csv.DictReader(csv_file, delimiter=";")) def move_files( files_csv: Path, clients_csv: Path, desktop_directory: Path | None = None, ) -> tuple[int, int, int]: desktop_directory = desktop_directory or default_desktop() client_rows = read_csv(clients_csv) client_names = { row.get("ParticipantName", "").strip(): row.get("Kunde", "").strip() for row in client_rows if row.get("ParticipantName", "").strip() and row.get("Kunde", "").strip() } moved_count = 0 skipped_count = 0 missing_client_count = 0 for row in read_csv(files_csv): participant_name = row.get("ParticipantName", "").strip() source = Path(row.get("FilePath", "").strip()) customer = client_names.get(participant_name) if not customer: missing_client_count += 1 continue if not source.is_file(): skipped_count += 1 continue target_directory = desktop_directory / "PC-Visit" / customer target_directory.mkdir(parents=True, exist_ok=True) target = target_directory / source.name if source.resolve() == target.resolve() or target.exists(): skipped_count += 1 continue shutil.move(str(source), str(target)) moved_count += 1 return moved_count, skipped_count, missing_client_count