Save variables with python

I was creating a simple game in python, i wanted to know a way to save it data in a txt like player name and his score and also put that in a leaderboard, how could i do that ?


You could have a look at the shelve module. It works like a python dictionary, allowing you to save data and retrieve it later.


If you do specifically want to write to a .txt file, you can use the standard Python functionality:

1 Like

For apps I usually prefer writing xml or json files for persistence. But in case you want to create something which looks more like a database, I would go for sqlite. This creates a local db, which can later be replaced by another SQL database running on a Database Server. It is lightweight, highly compressed, fast and can deal with multi access. So you can store your Highscore in the Web. It’s not as straightforward as creating a simple file, but a great entrypoint for databases.