user3562793
user3562793

Reputation: 61

How to use pickle to save data to disk?

I'm making a Animal guessing game and i finish the program but i want to add pickle so it save questions to disk, so they won't go away when the program exits. Anyone can help?

Upvotes: 5

Views: 5381

Answers (1)

AsksAnyway
AsksAnyway

Reputation: 930

Save an object containing the game state before the program exits:

pickle.dump(game_state, open('gamestate.pickle', 'wb'))

Load the object when the program is started:

game_state = pickle.load(open('gamestate.pickle', 'rb'))

In your case, game_state may be a list of questions.

Upvotes: 16

Related Questions