简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM