import os,sys from shutil import copyfile filelist=[] pathepub="" pathfile=[] pathfileexistant=[] temp=os.path.realpath(__file__).replace("\\", "/").rsplit("/",1) argument1= temp[0] print ("Repertoire en cours : " + argument1) question = (input ("Est ce le bon repertoire? O ou o puis entrée pour continuer \n")) if question == "O" or question == "o": print ("Repertoire défini : " + argument1) else: argument1="" argument1=(input('Tapez le chemin vers les epub ou rien pour sortir : (comme J:\\stock\\ebook )\n')).replace("\\", "/") if not len(argument1): sys.exit() else: print ("Repertoire utilisé : " + argument1) if os.path.isdir(argument1): pathepub=argument1.replace("\\", "/") fileindir = os.listdir(argument1) for file in fileindir: if os.path.isfile(pathepub+"/"+file) and ( file.endswith(".epub") or file.endswith(".EPUB") or file.endswith(".pdf") or file.endswith(".PDF")) : filelist.append(file) if file.find("-")>0: tempsplit=file.rsplit("-",1) if tempsplit[0].find("et"): temp = tempsplit[0].split("et") pathfile.append(pathepub + "/" + temp[0]) else: if tempsplit[0].find("&") : temp = tempsplit[0].split("&") pathfile.append(pathepub + "/" + temp[0]) else: pathfile.append(pathepub + "/" + tempsplit[0]) else: pathfile.append(pathepub + "/" + "xxxxx_Sans_tiret_xxxxxx") if os.path.isdir(pathepub+"/"+file): pathfileexistant.append(pathepub + "/" + file) if not len(filelist): sys.exit() ligne=0 for epubfile in filelist: fait=True if (len (pathfileexistant)): for pathexistant in pathfileexistant: if pathexistant.find("[")>0 and fait: temp = pathexistant.split("[") if (temp[0] == pathfile[ligne]): copyfile(pathepub + "/" + epubfile, pathexistant + "/" + epubfile) ligne+=1 fait=False if (fait): os.makedirs(pathfile[ligne], exist_ok=True) copyfile(pathepub + "/" + epubfile, pathfile[ligne] + "/" + epubfile) print(epubfile + " copié dans : " + pathfile[ligne] + "/" + epubfile) ligne+=1 listerep = os.listdir(pathepub) for rep in listerep: if os.path.isdir(pathepub+"/"+rep): listefile = os.listdir(pathepub+"/"+rep) nbepub = (len (listefile)) if rep.find("[")>0: temp=rep.split("[") os.rename(pathepub+"/"+rep, pathepub+"/"+temp[0]+"["+ str(nbepub)+"]") print ("Répertoire recompté : " + pathepub+"/"+temp[0]+"["+ str(nbepub)+"]") else : os.rename(pathepub+"/"+rep, pathepub+"/"+rep+"["+ str(nbepub)+"]") print ("Répertoire crée: " + pathepub+"/"+rep+"["+ str(nbepub)+"]") nbepub=0 print ("Nombre de fichier epub copiés : " + str(ligne)) input()