0

I am creating the exe of game made with pygame using pyinstaller. I am storing the highscore in a text file. The command is -

pyinstaller main.py --windowed --onefile --add-data "assets;assets"

The assets folder has the text file. Now the highscore functionality works fine for one session. But when I close the exe and open it again, the file resets to its original state.

Is there any way to prevent this?

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
SuPythony
  • 875
  • 8
  • 23
  • Have you tried compiling without the `add-data` tag and just kept the application in the original directory? – SlavaCat Oct 28 '21 at 04:27
  • PyInstaller will extract all your resources into a temporary location and then clean them up once the program exits. This is why the high score file resets. You should create a persistent file in `%APPDATA%\`. See this [question](https://stackoverflow.com/q/10563148/2280890) for more details. – import random Oct 28 '21 at 04:29
  • Ok, thank you! Will try and then revert. – SuPythony Oct 28 '21 at 04:30
  • @SlavaCat That would not keep it onefile – SuPythony Oct 28 '21 at 04:30
  • It would keep the executable in one file, but data would be stored elsewhere. I much prefer this method, but if it doesn't work for you, you might want to try import random's solution. – SlavaCat Oct 28 '21 at 04:34
  • Ok! @importrandom if I do that I would need to provide the users an installer to create the folder. They will not be able to directly use the exe. – SuPythony Oct 28 '21 at 04:36
  • @SlavaCat If I do doing this the user will be able to change the highscore. – SuPythony Oct 28 '21 at 04:37
  • Your application will need to handle creation of the highscores file if it doesn't exist. If you're on Windows, you could store information in the Registry using the in-built [winreg](https://docs.python.org/3/library/winreg.html) module. – import random Oct 28 '21 at 04:41
  • Encryption? I really don't know a whole lot about pyinstaller and the method I suggested is just what I do when I encounter issues like this. Sorry if I'm not much of a help. – SlavaCat Oct 28 '21 at 04:43
  • @importrandom So should I prefer using the registry or the AppData folder? – SuPythony Oct 28 '21 at 05:09
  • 1
    Thank you! I resorted to using the appdata directory using appdirs module. – SuPythony Oct 28 '21 at 05:22
  • A file is probably easier to get working. If you're using a dictionary to store scores, you can use built-in `json.loads` and `json.dumps` to process the string data you read/write to your file. You can also answer your own question so that future seekers who stumble upon this question have an answer. – import random Oct 28 '21 at 05:47
  • @importrandom Ok! Is it viable to use a `.db` file with python's sqlite3 module, or should I just use a file? – SuPythony Oct 28 '21 at 06:30
  • A file may be simpler, but a database can be a more elegant solution. Maybe this [question](https://stackoverflow.com/q/67356620/2280890) will get you started. – import random Oct 29 '21 at 03:46
  • @importrandom Ok! Thank you for you help! – SuPythony Oct 29 '21 at 05:18

0 Answers0