Reubicados los archivos en carpetas
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
import os
|
||||
import re # regexp
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def first_letter(x):
|
||||
if len(x) == 0:
|
||||
return "0-9"
|
||||
if x[0] in ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]:
|
||||
return "0-9"
|
||||
else:
|
||||
return x[0].upper()
|
||||
|
||||
|
||||
source_path = [Path("/home/sergio/zx/Games"), Path("/home/sergio/zx/Pokes")]
|
||||
destination_path = Path("/home/sergio/tmp/final")
|
||||
|
||||
opt_print = "no"
|
||||
opt_create_dirs = "yes"
|
||||
|
||||
# Elimina el directorio de destino
|
||||
try:
|
||||
print("Directory: {} -> deleting...".format(destination_path))
|
||||
shutil.rmtree(destination_path)
|
||||
print("Directory: {} -> removed successfully".format(destination_path))
|
||||
except OSError as o:
|
||||
print(f"Error, {o.strerror}: {destination_path}")
|
||||
|
||||
# Crea el directorio de destino
|
||||
try:
|
||||
os.mkdir(destination_path)
|
||||
print("Directory: {} -> created successfully".format(destination_path))
|
||||
except OSError as error:
|
||||
print(error)
|
||||
|
||||
# Variables
|
||||
paths = [] # Ruta donde se encuentra el fichero
|
||||
files = [] # Nombre del fichero
|
||||
names = [] # Nombre del juego
|
||||
years = [] # Año del juego
|
||||
companies = [] # Compañía o distribuidora del juego
|
||||
|
||||
# Obtiene la lista de ficheros desde los directorios de origen
|
||||
for path in source_path:
|
||||
for file_name in os.listdir(path):
|
||||
if os.path.isfile(os.path.join(path, file_name)): # Comprueba si es un fichero
|
||||
paths.append(path) # Añade la ruta
|
||||
files.append(file_name) # Añade el nombre del fichero
|
||||
|
||||
# Extrae los datos del juego
|
||||
regex_year = r"\(\d.*?\)"
|
||||
regex_company = r"^\(.*?\)"
|
||||
|
||||
for i in files:
|
||||
# Año
|
||||
match = re.search(regex_year, i) # Busca el año en el nombre del fichero
|
||||
if match:
|
||||
years.append(match.group()) # Añade el año con los parentesis a la lista
|
||||
pos = i.find(years[-1]) # Busca el caracter donde empieza el año
|
||||
names.append(
|
||||
i[0:pos].strip()
|
||||
) # Añade como nombre el texto que hay desde el principio hasta el año
|
||||
match_company = re.search(
|
||||
regex_company, i[pos + len(years[-1]) :]
|
||||
) # Busca la compañia en lo que queda despues del año
|
||||
if match_company:
|
||||
companies.append(match_company.group())
|
||||
else:
|
||||
companies.append("-")
|
||||
|
||||
# Limpia los parentesis del año y la compañía
|
||||
years[-1] = years[-1][1:5]
|
||||
if companies[-1] != "-":
|
||||
companies[-1] = companies[-1][1:-1]
|
||||
else:
|
||||
years.append("0")
|
||||
names.append(i)
|
||||
companies.append("-")
|
||||
|
||||
# Lista los resultados
|
||||
if opt_print == "yes":
|
||||
for i, item in enumerate(files):
|
||||
print(
|
||||
"File: {}\nName: {}\nYear: {}\nCompany: {}\n".format(
|
||||
item, names[i], years[i], companies[i]
|
||||
)
|
||||
)
|
||||
|
||||
# Copia los archivos
|
||||
total_files = len(files)
|
||||
if opt_create_dirs == "yes":
|
||||
for i in range(total_files):
|
||||
print("({} de {}) {}".format(i + 1, total_files, files[i]))
|
||||
game_dir = names[i] + " (" + years[i] + ")"
|
||||
dst_path = os.path.join(destination_path, first_letter(names[i]), game_dir)
|
||||
if not os.path.exists(dst_path):
|
||||
os.makedirs(dst_path)
|
||||
src = os.path.join(paths[i], files[i])
|
||||
dst = os.path.join(dst_path, files[i])
|
||||
shutil.copyfile(src, dst)
|
||||
Reference in New Issue
Block a user