This is my code;
Import os
#hard code the path to the external file
external_file = 'names.txt'
username = 'testuser'# Our hardcoded name
#if file doesn't exists, create a new file
if not os.path.exists(external_file):
#Ask the user's name
name = raw_input("What's your name?")
with open(external_file, "a") as f: # using "a" will append to the file
f.write(name)# Write the name to names.txt
f.write("\n")
f.close()
else:
#if file exists, use it to load name, else ask user
with open(external_file, "r+") as f:# r+ open a file for reading & writing
lines = f.read().split('\n') # split the names
print lines
if username in lines: #Check if the file has any username as 'testuser'
print "Hi {}".format(username)
else: # If there is no username as 'testuser' then ask for a name
name = raw_input("What's your name?")
f.seek(0,2) # Resolves an issue in Windows
f.write(name)# Write the name to names.txt
f.write("\n")
f.close()
This is good and working code but I don't want it to have a list of names being displayed and it asking you to pick. I want it to store only one name and draw that name everytime the programme is turned on unless you tell it otherwise E.g;
IsName = raw_input("That is your name, right?")
If 'no' in IsName:
Then the code to change the name on file