New File Detection:Python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

New File Detection:Python

I am making a game where if you cheat, you get permanently banned from the game. It does this through a small file called "playStats.dat". I make it using pickle. I wanted to see what happened if I deleted the playStats.dat file, to see what it would be like for new users. But when I ran the program without the playStats file it failed. I recreated the file using a Time Machine backup. Is there any way I could make it so that if the file is missing, it creates a new one that says that the player can play? That would be great, but if needed, I could just put the playStats.dat file in my download once I upload the file. If you want, I can make a SoloLearn version of my program. Just remember that right now it's not a game, I am just making the skeleton. I am new to Python, and I don't understand this.

10th Feb 2019, 3:22 PM
Anthony Morassutti
Anthony Morassutti - avatar
4 Answers
+ 6
alternative way: import os if not os.path.exists('playStats.dat'): with open('playStats.dat', 'w'): pass
10th Feb 2019, 4:09 PM
Hubert Dudek
Hubert Dudek - avatar
+ 1
Open the file in 'w+b' mode. instead of: load_file = open('playStats.dat', 'rb') use: load_file = open('playStats.dat', 'w+b')
10th Feb 2019, 3:36 PM
Ulisses Cruz
Ulisses Cruz - avatar
10th Feb 2019, 3:24 PM
Anthony Morassutti
Anthony Morassutti - avatar
thank you!
15th Feb 2019, 11:27 PM
Anthony Morassutti
Anthony Morassutti - avatar