3
私はPythonでコンソールアプリケーションで作業しています。プログラム状態をjsonファイルとして保存するコマンドがありますが、ファイルを書き込むと空です。 .as_list()の結果は、任意の3リストを含むリストです。Pythonは空のjsonファイルを書き込みます
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] + "\"")
あなたのプログラムはそのファイルに書き込む権限を持っていますか? –