[英]Print array content when for finishes
我希望在下載種子后整理我的種子。 我編寫了這個腳本來檢查系列名稱和劇集,並將文件移動到我保存電視節目的其他磁盤。 我希望在運行時打印一些進程。 我遇到了其中一個問題。 我想首先打印“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')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.