7

I'm working on a console app in python. I have a command that should save the program state as a json file, but when I write the file, it's empty. The result of .as_list() here is a list containing an arbitrary 3-lists.

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] + "\"")
1
  • Does your program have permission to write to that file? Commented Apr 27, 2016 at 22:35

1 Answer 1

13

json.dumps doesn't write a file, it returns the string serialized as JSON. You're looking for json.dump. Notice the missing fp param for dumps.

Sign up to request clarification or add additional context in comments.

1 Comment

20 min lost on this one

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.