Ask coding questions

← Back to all posts
How do I edit a file with a specific name according to the user
JW2005

So after I made this code, it will open a new file but the code cannot write anything into the text file I have opened. Any ways that I can do this. It is sort of like when a user inputs his or her username, the text file will open accordingly, and what the user has typed or edit, the file will append and write them in the text file. But my code doesn't work. TT

Voters
JW2005
Comments
hotnewtop
VulcanWM

I suggest you use a database instead of editing files because you can only do that when you have editing perms for the repl so any other user doing the program won’t be able to have the full experience

JW2005

@VulcanWM This is just a part of my future code. I wanted to make somesort like a diary with a login n register code and the user can edit in the console and things will be saved into their own textfile so that other ppls things wont be in the same file. So it would be like taking their name as a filename, then the things they enter will only go into the specific filename.

VulcanWM

but a user who doesnt have access to edit the repl cannot edit, make or delete a file in the code
@JW2005

JW2005

@VulcanWM I mean like saving their data in a file. For example in a game. When they have coins and certain items, they need to save it or else they will lose em. So, I am planning to make files for each user when they registered, and when they earn more coins, the code will edit the new coin number and automatically save it in the file. So it will be like,

coins = 1000
if player earn coins, coins += 1100
then before they exit,
a = open(username+".txt","w")
a.write(coins)
a.close

they will have their original coin value stored in that file and then when befoer they exit the new coin number will be editted through the a.write() and being saved

VulcanWM

yeah that’s still
editing a file
@JW2005

JW2005

@VulcanWM so how do I use database to save different users item n coins??? Cuz there might be more than 10 users using that.... and how do i even set the database and let it function. how can i access the data in databases...

VulcanWM

You can use replit db which is replit’s own database
db[‘key’] = ‘value’ to set a value
value = db[‘key’] to get a value of a key from the database
del db[‘key’] to delete a key and value from the database
print(db.keys()) to get all of the keys and values in the database
and
from replit import db
to actually start the db @JW2005

JW2005

@VulcanWM alright... thank you very much

JW2005

@VulcanWM but can I save multiple users information without creating more than one key??

VulcanWM

Yes you can do
db[‘Users’] = {‘user1’: {‘coins’: 0, ‘lives’: 5}, ‘user2’: {‘coins’: 5, ‘lives’: 0}}
@JW2005