我正在编写一个脚本来组织文件夹中的文件。如果文件位于根文件夹中,则脚本可以正常运行,但是当我添加一行以运行以检查子文件夹中的文件时,该脚本无法正常工作
#!/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]*"
for root, dirs, files in os.walk(dir_temp):
for series in files:
if series.endswith(('.mp4', '.srt', '.avi', '.mkv')):
arch_encontrados = len(series)
print(colored("\nArchivos encontrados:",'red', attrs=['bold'] ), colored(arch_encontrados, 'red', attrs=['bold'] ),'\n')
print(*series, sep = "\n")
for serie in series:
#Extraer el nombre de la serie
nombre = re.findall(r'.*[\. ][sS]\d', 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(path)
else:
path = os.path.join(dir_series, series_path, 'Season ', season_final_numero)
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'), serie, colored('a', 'green'), path + '/' + serie)
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']))
当我在根目录中执行此操作时,效果很好
dir_series = "/home/user/series/series/"
buscar = "*[sS][0-9][0-9]*"
series = [s for s in glob.glob(buscar) 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')
print(*series, sep = "\n")
有什么想法吗?谢谢
/编辑: 我相信我已经解决了。现在,当我尝试执行最后一次shutil.move时,我又遇到另一个错误
print(colored('\nCopiando','green'), serie, colored('a', 'green'), path + '/' + serie)
NameError: name 'path' is not defined
新代码:
#!/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
coincidencia = False
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
coincidencia = True
coincidencia_season = False
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
coincidencia_season = True
path = os.path.join(dir_series, series_path, season_final)
print(path)
if not coincidencia_season:
path = os.path.join(dir_series, series_path, 'Season ' + season_final_numero)
print(path)
if not coincidencia:
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'), serie, colored('a', 'green'), path + '/' + serie)
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']))