0

I'm having some problems with a script. It's suppose to organize some files checking for their extension and some file name. The problem is with a for cycle for series_path in os.listdir(dir_series:, the for goes through a list of folders and perform some action. First check for the folder to exist not caring for uppercase, then does the same for a folder withing that folder. An if cycle comes into play to check some conditions. That works, the problem is that the else

else: print(colored("\n\n*****************************************",'cyan', attrs=['bold'])))

of the first if keeps running for every file that it finds in the for cycle trying to create a folder and it should only be done the amount of times it finds a file with a certain extention.

#!/usr/bin/env python3
import sys, glob, re, os, shutil
from termcolor import colored
#enconding: utf-8

dir_series = "/home/user/series/series/"
dir_temp = "/home/user/stuff/shared/temp/"

buscar = "*[sS][0-9][0-9]*"

series = [os.path.join(root, s) for root, dirs, files in os.walk(dir_temp) for s in files if s.endswith(('.mp4', '.srt', '.avi', '.mkv'))]
if series:
   arch_encontrados = len(series)
   print(colored("\nArchivos encontrados:",'red', attrs=['bold'] ), colored(arch_encontrados, 'red', attrs=['bold'] ),'\n')
   for series_base in series:
    print(os.path.basename(series_base), sep = "\n")

   for serie in series:
    #Extraer el nombre de la serie
    nombre = re.findall(r'.*[\. ][sS]\d', os.path.basename(serie))[0]
    nombre_final = re.sub(r'[\. ][sS]\d','',nombre).replace('.',' ')
    #Extraer el número de la temporada
    season = re.findall(r'[\. ][sS]\d\d', serie)[0]
    season_final_numero = re.sub(r'[\. ][sS]','',season)
    season_final = ('season ' + season_final_numero)
    #Armar el directorio final
    for series_path in os.listdir(dir_series): #lista el contenido de /home/user/series/series/
        if nombre_final.lower() == series_path.lower(): #compara el listado con la salida del nombre de la serie sin importar mayúsculas y minúsculas
            for season_path in os.listdir(dir_series + series_path):
                if season_final == season_path: #compara el listado de seasons contra season_final que tiene mayuscula
                    path = os.path.join(dir_series, series_path, season_final)
                    print('1')
                else:
                    path = os.path.join(dir_series, series_path, 'Season ' + season_final_numero)
                    print('2')
        else: 
            print(colored("\n\n*****************************************",'cyan', attrs=['bold']))  
            print(colored("** Directorio no encontrado, creándolo **",'cyan', attrs=['bold']))  
            print(colored("*****************************************\n",'cyan', attrs=['bold']))  
            path = os.path.join(dir_series, nombre_final, season_final)
            print(path)
            os.makedirs(path) 

    #Mover el archivo
    print(colored('\nCopiando','green'), os.path.basename(serie), colored('a', 'green'), path)
    shutil.move(serie,path)

else:
    print(colored('\nNo hay archivos para organizar.','green', attrs=['bold']))


input(colored("\n\nPresione Enter para continuar ...", attrs=['blink', 'bold']))

Any ideas how to change the script to do that?

elRestaurador
  • 61
  • 1
  • 5
  • maybe you should display it before loop. Or maybe it should be in other place. – furas Dec 11 '19 at 03:18
  • The `else:` will run for every file in the directory that doesn't match `nombre_final.lower()`, even if there is another file that matches. – Barmar Dec 11 '19 at 03:37

0 Answers0