From 89e12b5f1f1ff3e31029fa54d53ed7fc1c5b2445 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Thu, 3 Jun 2021 21:58:14 +0200 Subject: [PATCH] utilisation fichier csv et ajout groupe de chiffres --- main.py | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index 2d04fc1..d9b7e33 100644 --- a/main.py +++ b/main.py @@ -1,19 +1,45 @@ -from os import walk +import re import sys from pathlib import Path +from os import walk def listeDossier(path = None): + """Ecris tous les noms des fichiers d'un dossier dans un fichier csv, en séparant les groupes de chiffres""" + # récupération path if path == None: path = Path().absolute() else: path = Path(path) + # récupération des fichiers _, _, filenames = next(walk(path)) - with open(path.parent.absolute() / "resultat.txt", 'w') as f: - f.write('\n'.join(filenames)) + # création du fichier résultat s'il n'existe pas + resultatPath = path.parent.absolute() / "resultat.csv" + if not isFileExists(resultatPath): + open(resultatPath, "x") + # ajout des groupes de chiffres dans les colomnes du csv + for i in range(0, len(filenames)): + ID = re.findall(r'\d+', filenames[i]) + for j in range(0, len(ID)): + filenames[i] = f"{filenames[i]};{ID[j]}" + + # ajout des résultat au fichier + with open(resultatPath, 'a') as f: + f.write('\n'.join(filenames)) + f.write('\n') + return "Done!" +def isFileExists(path): + """Vérifie qu'un fichier existe""" + try: + open(path, "r") + except FileNotFoundError: + return False + else: + return True + if __name__ == "__main__": print(listeDossier(sys.argv[1] if len(sys.argv) > 1 else None))