basic .json storage and retrieval
I'm trying to write a program that takes in a person's name and stores it in a .json file. If that person has already run the program on their computer then the program will check the .json file and read that the user has already entered their name, and greet them.
If not, the program will prompt them for their name. I think I have it mostly correct. Here's the code:
import json def get_stored_username(): """retrieve username of old user""" filename = 'text_files\filenameu.json' try: with open(filename) as usernames: username = json.load(usernames) except FileNotFoundError: return None else: return username def greet_user(): """greet the user by name""" username = get_stored_username() if username: print("Welcome back, " + username + "!") else: username = input("What is your name? ") filename = 'text_files/filenameu.json' with open(filename, 'w') as usernames: json.dump(username, usernames) print("we'll remember you next time you come back, " + username + "!") greet_user()
and the error:
Traceback (most recent call last): File "jasoncont.py", line 27, in <module> greet_user() File "jasoncont.py", line 16, in greet_user username = get_stored_username() File "jasoncont.py", line 7, in get_stored_username with open(filename) as usernames: OSError: [Errno 22] Invalid argument: 'text_files\x0cilenameu.json'
Any help would be greatly appreciated. Thanks!