I have this function from a game class:
def store_data(self):
if self.score > self.best_score:
with open("best_score.txt", "w") as file:
msg = f"BEST SCORE: {self.score}"
file.write(msg)
file.close()
I want to store the best_score in this txt file (if there is a better way of storing data in python you're welcome to correct me, thank you) I finished the game and I used pyinstaller + NSIS to get a single file, and of course, I installed it on my device, before installing there is no issue, but after installing and running the game from my local desk C:\\ I got this error:
PermissionError: [Errno 13] Permission denied: 'best_score.txt'
So please, could you help me. Thank you.
The user executing the code doesn't have the required permission. Maybe you're running the code in a directory which requires admin privilege (and since you do not provide a path, Python tries to save the file in that dir) or maybe another user created the file.
I recommend storing the result in (a sub-path of) the user's home directory, e.g.
import os
USER_HOME: str = os.path.expanduser("~")
with open(f"{USER_HOME}/best_score.txt", "w") as file:
# do stuff