aplicar barra de progresso durante copia de ficheiros com módulo tqdm
This commit is contained in:
@ -1,8 +1,13 @@
|
||||
# copier.py
|
||||
# Funções para copiar ficheiros para o destino organizado
|
||||
from . import organizer
|
||||
from modules.organizer import (
|
||||
obter_data_ficheiro,
|
||||
criar_pasta_destino,
|
||||
categorizar_por_tipo,
|
||||
)
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
from tqdm import tqdm
|
||||
|
||||
|
||||
def copiar_ficheiros_para_destino(
|
||||
@ -25,18 +30,38 @@ def copiar_ficheiros_para_destino(
|
||||
retorna:
|
||||
None
|
||||
"""
|
||||
destino_path = Path(base_destino)
|
||||
with tqdm(
|
||||
total=len(lista_ficheiros), desc="A copiar ficheiros ", unit="ficheiro "
|
||||
) as barra_progresso:
|
||||
for ficheiro in lista_ficheiros:
|
||||
if not Path(ficheiro).is_file():
|
||||
print(f"Aviso: '{ficheiro}' não existe ou não é um ficheiro.")
|
||||
ficheiro_path = Path(ficheiro)
|
||||
if not ficheiro_path.exists():
|
||||
print(f"Aviso: ficheiro '{
|
||||
ficheiro}' não encontrado. A ignorar.")
|
||||
barra_progresso.update(1)
|
||||
continue
|
||||
categoria = organizer.categorizar_por_tipo(ficheiro)
|
||||
data = (
|
||||
organizer.obter_data_ficheiro(ficheiro, usar_criacao)
|
||||
if categorizar_data
|
||||
else None
|
||||
|
||||
if categorizar_data:
|
||||
data_ficheiro = obter_data_ficheiro(
|
||||
ficheiro, usar_criacao=usar_criacao)
|
||||
pasta_destino = criar_pasta_destino(
|
||||
str(destino_path),
|
||||
categorizar_por_tipo(ficheiro),
|
||||
data_ficheiro,
|
||||
formato_data,
|
||||
)
|
||||
pasta_destino = organizer.criar_pasta_destino(
|
||||
base_destino, categoria, data, formato_data
|
||||
else:
|
||||
pasta_destino = criar_pasta_destino(
|
||||
str(destino_path),
|
||||
categorizar_por_tipo(ficheiro),
|
||||
)
|
||||
destino_final = pasta_destino / Path(ficheiro).name
|
||||
shutil.copy2(ficheiro, destino_final)
|
||||
|
||||
destino_ficheiro = pasta_destino / ficheiro_path.name
|
||||
|
||||
try:
|
||||
shutil.copy2(ficheiro_path, destino_ficheiro)
|
||||
except Exception as e:
|
||||
print(f"Erro a copiar '{ficheiro}': {e}")
|
||||
|
||||
barra_progresso.update(1)
|
||||
|
||||
Reference in New Issue
Block a user