我正在使用python中的控制台应用程序。我有一个命令应该将程序状态保存为json文件,但是当我写文件时,它是空的。 .as_list()的结果是包含任意3个列表的列表。
class SaveCommand():
def __init__(self, console):
self.console = console
def execute(self, args):
if self.console.combat is None:
raise ConsoleCommandException("there is no active combat")
if len(args) != 1:
raise ConsoleCommandException("save [save name]")
try:
with open("save_files/" + args[0] + ".json", "w") as outfile:
json.dumps(self.console.combat.as_list(), outfile)
except Exception:
raise ConsoleCommandException("save failed: unknown error")
print("successfully saved \"" + args[0] + "\"")