import shutil from pathlib import Path from concurrent.futures import ThreadPoolExecutor import py7zr Dossier = "/media/remy/T7/IGNDonnee/" SOURCE = Path(Dossier) SORTIE = Path(Dossier) TYPES = ["DST", "MNT", "SRC"] NB_THREADS = 4 for t in TYPES: (SORTIE / t).mkdir(exist_ok=True) (SOURCE / "FAIT").mkdir(exist_ok=True) temp = Path("_temp_rgealti") if temp.exists(): shutil.rmtree(temp) def compresser_fichier(args): fichier, dossier_sortie = args archive_sortie = dossier_sortie / f"{fichier.name}.7z" with py7zr.SevenZipFile(archive_sortie, "w") as z: z.write(fichier, arcname=fichier.name) archives = list( SOURCE.glob("RGEALTI_2-0_1M_ASC_LAMB93-IGN69_D0*.7z") ) with ThreadPoolExecutor(max_workers=NB_THREADS) as pool: for archive in archives: print(archive) if temp.exists(): shutil.rmtree(temp) temp.mkdir() # Extraction archive source with py7zr.SevenZipFile(archive, "r") as z: z.extractall(temp) print("fin Extraction") taches = [] for t in TYPES: for dossier in temp.rglob(f"RGEALTI_{t}_1M_*"): if dossier.is_dir(): for fichier in dossier.iterdir(): if fichier.is_file(): taches.append((fichier, SORTIE / t)) list(pool.map(compresser_fichier, taches)) # Déplacement archive traitée shutil.move( str(archive), str(SOURCE / "FAIT" / archive.name) ) if temp.exists(): shutil.rmtree(temp)