I would like to use this in a program to save users' names even after they relaunch the script. I was just wondering if there were some ways I could clean up this code.
Note: this currently was made to not support name changing, but could possibly be implemented in the future.
if os.path.isfile('names.json'): # if there is a file with names...
text_file = open("names.json") # only open it in normal mode
else: # if not...
text_file = open("names.json", "w+") # make one
if os.stat("names.json").st_size > 0:
for line in text_file:
if line.istitle():
player_name = line
else:
player_name = console.input_alert('What is your name? ').title()
text_file = open("names.json", "w")
text_file.write(player_name)
text_file.close()
else:
player_name = console.input_alert('What is your name? ').title()
text_file = open("names.json", "w")
text_file.write(player_name)
text_file.close()