简体   繁体   中英

How do I not overwrite my save data? (pickle in python)

While using pickle, I came across a problem (for me) where it overwrites the save data with the default save data when the script is open up. Is there a way to detect if anything is in the file?


import pickle

savedata = ['tutorialisntfinished', 'tutorialisfinished', 'level1isntfinished', 'level1isfinished', 'level2isntfinished', 'level2isfinished', 'bossfightisntfinished', 'bossfightisfinished']
pickle.dump(savedata, open('savedata.json', 'wb'))    

And then I have some scripts that detect scenes and change variables.

I have a system to change save data by removing the (example)isntfinished from the list called savedata. All of this works, but When you reopen the script, it resets it. Anyone have a fix / solution for this?

Do this:

try:
    with open("fname", "xb") as fout:
        #Work with your open file
except FileExistsError:
    # Your error handling goes here

I can recommend you reading What does python3 open "x" mode do?

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