Ask coding questions

← Back to all posts
Database-Like File
KiKUP (42)

Hi there repl.it users, I just had a question that I needed answering for a game I'm making. I would like to store the amount of points someone gets in one game in a file after someone quits the game. In addition, I would like to make this file unchangeable by any users except me. How can I do this?

Commentshotnewtop
TheDrone7 (777)

The files in your repl can only be modified by you. As for the first part, I recommend using an external DB instead of files, since the data in the files will get reset everytime your repl is run.

KiKUP (42)

@TheDrone7 How can I get someone's else's score in my file on my computer, though?

TheDrone7 (777)

@KiKUP well, depending on the language, you just write to the files to save their data and read the files to fetch the data. You can save the data in JSON format to keep it organized.

AllAwesome497 (330)

What language? If u know how to do it make a file called .env

AllAwesome497 (330)

@KiKUP ok. If u want it hidden to other users:
1) make a file named .env
2)add import ast to the beginning of your code
3) Read the file:

with open('.env', 'r') as f:
    <item name> = ast.literal_eval(f.read())

4)Write data into file:

with open('.env', 'w') as f:
    f.write(str(<item name>))

here's an example of something I did with this: https://repl.it/@AllAwesome497/LOL-Bot

KiKUP (42)

@AllAwesome497 Thank you, I'll try this out.

coder787878 (4)

You can use .txt files in Python and append.

coderash (70)

@coder787878 Yeah thats an option, but it doesn't work 100%

kpostal10 (20)

As stated by TheDrone7 It is highly recommended to use some sort of external database. If you try to use a file on repl while sometimes it will work and save the data it will get corrupt at some point. As for making it unchangeable by any users, handling it server side and protecting it and using some sort of authentification system with tokens might be something you could look into.

Vandesm14 (2167)

@kpostal10 But isn't an external database technically just a file database with an API?