Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use pickle to save data to disk?

Tags:

python

pickle

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?

like image 438
user3562793 Avatar asked Dec 08 '22 08:12

user3562793


1 Answers

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.

like image 104
AsksAnyway Avatar answered Dec 11 '22 11:12

AsksAnyway