Spaces:
Sleeping
Sleeping
File size: 1,657 Bytes
f50ffa8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
from pathlib import Path
from typing import List, Dict
def load_data(path: Path) -> List[Dict[str, str]]:
"""
Legge tutti i file in una cartella e restituisce una lista di dizionari
con title (nome file) e content (contenuto file).
Args:
path (str): Percorso della cartella da leggere
Returns:
List[Dict[str, str]]: Lista di dizionari con 'title' e 'content'
Raises:
FileNotFoundError: Se il path non esiste
NotADirectoryError: Se il path non è una cartella
"""
folder_path = Path(path)
# Verifica che il path esista e sia una cartella
if not folder_path.exists():
raise FileNotFoundError(f"Il path '{path}' non esiste")
if not folder_path.is_dir():
raise NotADirectoryError(f"Il path '{path}' non è una cartella")
documents = []
# Cicla attraverso tutti i file nella cartella
for file_path in folder_path.iterdir():
# Salta le sottocartelle, processa solo i file
if file_path.is_file():
try:
# Legge il contenuto del file
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
# Crea il dizionario per questo file
document = {
'title': file_path.name, # Nome del file (senza path)
'content': content
}
documents.append(document)
except Exception as e:
print(f"[ERROR] Errore nella lettura del file '{file_path}': {e}")
return documents |