Я делаю программу для изменения профилей терминала windows, и они находятся в файле JSON. Ну, я хочу поставить другой профиль, используя python (профиль словарь), и я получаю следующую ошибку:
Traceback (most recent call last):
File "C:\Users\DELLNEGRA\Desktop\wtedit.py", line 31, in <module>
datos["profiles"][len(datos["profiles"])]["guid"] ="{"+pguid+"}"
IndexError: list index out of range
Код такой:
import json
import os
import secrets
import numpy as np
path = os.path.dirname(os.path.realpath(__file__))
file = open(path+"/profiles.json","r")
contenido = file.read()
file.close()
datos = json.loads(contenido)
print("Tenes "+str(len(datos["profiles"]))+" consolas registradas.")
print("Añadiendo otra consola...")
pguid = secrets.token_hex(4)+"-"+secrets.token_hex(2)+"-"+secrets.token_hex(2)+"-"+secrets.token_hex(2)+"-"+secrets.token_hex(6)
print("Guid generado: "+pguid)
pname = input("Ingrese el nombre de la consola:")
proot = input("Ingrese la ruta de la consola:")
#datos["profiles"][len(datos["profiles"])] = dict() #Here the error appears
datos["profiles"][len(datos["profiles"])]["guid"] ="{"+pguid+"}"
datos["profiles"][len(datos["profiles"])]["name"] =pname
datos["profiles"][len(datos["profiles"])]["name"] =proot
dumpdatos = json.dumps(datos, sort_keys=True, indent=4)
file = open(path+"/profiles.json","w")
file.write(dumpdatos)
file.close()
Как сделать Я поместил словарь туда без ошибок?
Профили. json Содержание:
{
"$schema": "https://aka.ms/terminal-profiles-schema",
"defaultProfile": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
"keybindings": [],
"profiles": [
{
"commandline": "powershell.exe",
"guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
"hidden": false,
"name": "Windows PowerShell"
},
{
"commandline": "cmd.exe",
"guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
"hidden": false,
"name": "CMD"
},
{
"commandline": "C:\\Users\\DELLNEGRA\\AppData\\Local\\Programs\\Python\\Python37-32\\python.exe",
"guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6102}",
"hidden": false,
"name": "Python"
},
{
"guid": "{2c4de342-38b7-51cf-b940-2309a097f518}",
"hidden": false,
"name": "Ubuntu",
"source": "Windows.Terminal.Wsl"
},
{
"commandline": "c",
"guid": "{e78a660a-660b-4d0e-2cc5-35707797c95e}",
"name": "c"
}
],
"schemes": []
}
Я пробовал с datos["profiles"][len(datos["profiles"])].append({"guid":"{"+pguid+"}","name":pname,"commandline":proot})
, но не работает