import json import os def extract_ids(): characters_dir = 'nodes/character_reader/characters' output_file = 'character_ids.txt' if not os.path.exists(characters_dir): print(f"Directory not found: {characters_dir}") return character_ids = [] # List all files in the directory files = os.listdir(characters_dir) # Sort files to ensure consistent output order files.sort() for filename in files: if filename.endswith('.json'): filepath = os.path.join(characters_dir, filename) try: with open(filepath, 'r', encoding='utf-8') as f: data = json.load(f) if 'character_id' in data: character_ids.append(data['character_id']) else: print(f"Warning: 'character_id' not found in {filename}") except json.JSONDecodeError: print(f"Error decoding JSON in {filename}") except Exception as e: print(f"Error reading {filename}: {e}") # Write to output file try: with open(output_file, 'w', encoding='utf-8') as f: for char_id in character_ids: f.write(f"{char_id}\n") print(f"Successfully wrote {len(character_ids)} character IDs to {output_file}") except Exception as e: print(f"Error writing to output file: {e}") if __name__ == "__main__": extract_ids()