#!/usr/bin/env python3
import os, csv, json

BASE = os.path.expanduser('~/netgescon/estratti')
result = {}

for root, dirs, files in os.walk(BASE):
    for file in files:
        if file.endswith('.csv'):
            path = os.path.join(root, file)
            try:
                with open(path, encoding='utf-8') as f:
                    reader = csv.reader(f)
                    header = next(reader)
                rel = os.path.relpath(path, BASE)
                result[rel] = header
            except Exception as e:
                result[rel] = f"ERRORE: {e}"

with open('schema_estratti.json', 'w', encoding='utf-8') as out:
    json.dump(result, out, indent=2, ensure_ascii=False)

print("Salvato schema_estratti.json con le intestazioni di tutti i file CSV trovati.")