python - 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 ...")
解决方案
您的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')
推荐阅读
- xml - 在字符串中查找重复的子字符串
- python - 从其“父级”合并价值项目和相关信息
- c++ - 为什么串行执行比并行执行花费更少的时间?
- swift - 如何将删除键添加到 NSCharacterSet
- nginx - nginx位置和proxy_pass的问题
- python - 将 Tensorflow 1.13 和 Keras 2.2.4 与 Anaconda 一起使用
- java - 我的 Spring Boot 项目应该 git 哪些文件
- symfony - Symfony 序列化:更改默认 DateTimeNormalizer 格式
- git - 了解 git 如何处理特定文件?
- dictionary - JuMPDict 维度变化