Python Forum
checking username - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: checking username (/thread-42320.html)



checking username - newbexx - Jun-17-2024

Hi, I'm reading Python Crashcourse (third edition), chapter 10 (about files).
The code is:

from pathlib import Path
import json

def get_stored_username(path):
    if path.exists():
        contents = path.read_text()
        username = json.loads(contents)
        return username
    else:
        return None
My (logical?) question is how does the code knows the username. In other words the code checks for the existence of a username.json but how does the code knows this file belongs to the user running the code?

Complete code:

from pathlib import Path
import json


def get_stored_username(path):
    """Get stored username if available."""
    if path.exists():
        contents = path.read_text()
        username = json.loads(contents)
        return username
    else:
        return None

def get_new_username(path):
    """Prompt for a new username."""
    username = input("What is your name? ")
    contents = json.dumps(username)
    path.write_text(contents)
    return username

def greet_user():
    """Greet the user by name."""
    path = Path('username.json')
    username = get_stored_username(path)
    if username:
        print(f"Welcome back, {username}!")
    else:
        username = get_new_username(path)
        print(f"We'll remember you when you come back, {username}!")

greet_user()



RE: checking username - menator01 - Jun-17-2024

Usually there are more than one user in a username file. You would do a comparison for said username.


RE: checking username - newbexx - Jun-17-2024

(Jun-17-2024, 05:34 AM)menator01 Wrote: Usually there are more than one user in a username file. You would do a comparison for said username.

Yes, but how do i know the correct username?


RE: checking username - Pedroski55 - Jun-17-2024

They should have written usernames, that would be less confusing for you perhaps.

Try like this maybe? Make your own file, then save it and then open it.

Of course, you need to set your own path, don't try to use my path!

import json
from pathlib import Path

path2json = '/home/pedro/temp/usernames.json'
mypath = Path(path2json)
data = []
username = 'X'
# enter usernames and emails
while not username == 'q':
    users_dict = {}
    username = input(f'Enter your username, enter q to quit ... ')
    if username == 'q':
            break
    email = input('enter your email ... ')
    users_dict['user name'] = username
    users_dict['user email'] = email
    data.append(users_dict)

# save the data
with open(mypath, 'w', encoding='utf-8') as fp:
    json.dump(data, fp, ensure_ascii=False, indent=4)

# open the data to inspect it
with open(mypath) as fp:
    data_list = json.load(fp)

# find Tarzan
for d in data_list:
    if d['user name'] == 'Tarzan':
        print(d)
        print(d['user name'])

Output:
{'user name': 'Tarzan', 'user email': '[email protected]'} Tarzan