2016-04-27 8 views
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] + "\"") 
+0

あなたのプログラムはそのファイルに書き込む権限を持っていますか? –

答えて

9

json.dumpsそれはJSONとしてシリアル化された文字列を返し、ファイルを書き込みません。あなたはjson.dumpを探しています。 dumpsの場合、fpパラメータが見つからないことに注意してください。

+0

20分が失われました – guilhermecgs

関連する問題