首页 > 解决方案 > for 完成时打印数组内容

问题描述

我希望在下载种子后整理我的种子。我编写了这个脚本来检查系列名称和剧集,并将文件移动到我保存电视节目的其他磁盘。我希望在运行时打印一些进程。我遇到了其中一个问题。我想首先打印“Archivos encontrados”(表示已创建文件),然后打印带有目录中所有文件的变量(在这种情况下,变量称为“ series”)问题是,当我写它时,它打印一个Archivos encontrados 为它找到的每个文件。正如您在第 21 行中看到的那样,我已经尝试检查文件结尾,但它不起作用。

此外,else如果找不到我声明的任何扩展名不起作用,最后应该运行的。

提前致谢

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

dir_series = "/home/user/series/series/"

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

for serie in glob.glob(buscar):
 if serie.endswith(('.mp4', '.srt', '.avi', '.mkv')):

  #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 = re.sub(r'[\. ][sS]','',season)

  #if serie == serie[-1]:
  print(colored("Archivos encontrados: ",'red'))
  print(serie)


  #Armar el directorio final
  path = os.path.join(dir_series, nombre_final, ('Season '+ season_final))

  #Chequear si el directorio existe
  if not os.path.exists(path):
   print(colored("\nDirectorio no encontrado, creándolo",'cyan'))  
   os.makedirs(path) 

  #Mover el archivo
  shutil.move(serie,path)
  print(colored('\nCopiando:','green'), serie, colored('a', 'green'), path + '/' + serie)


 else:
  print('No hay archivos para organizar.\n')

input("\n\nPresione Enter para continuar ...")

标签: python

解决方案


您的if serie == serie[-1]检查不起作用,因为不是检查“该系列是否是列表中的最后一个”,而是检查“该系列是否也是同一系列的最后一个字符”。

考虑改用这样的东西:

series = [s for s in glob.glob(buscar) if s.endswith(('.mp4', '.srt', '.avi', '.mkv'))]
if series:
    print(colored("Archivos encontrados: ",'red'))
    for serie in series:
        print(serie)
        ...
else:
    print('No hay archivos para organizar.\n')

推荐阅读