-4

My last question was asked wrong. If my code is

import os
#hard code the path to the external file
external_file = '/name.txt'
#if file exists, use it to load name, else ask user
if os.path.isfile(external_file):
    name = open(external_file).read()
else:
    name = raw_input("What's your name?")

How do i store the name in a .txt so it remembers the next time I start the programme but if i tell it 'that is not my name' it will return the 'what is your name' and store the name What am i doing wrong

timgeb
  • 64,821
  • 18
  • 95
  • 124
  • 2
    do you want to store the name in `external_file` or another file? also im not understanding the second part of your specification. where/when would you be supposed to enter "that's not my name"? – timgeb Jun 11 '14 at 21:32
  • Where it stores does not matter as long as it stores but for this , we use external_file and it is a code that will be online so you will customise it to your own standard so your own name per use. Like a bot. – user3729400 Jun 11 '14 at 21:35
  • Two notes: 1) about this question: you show the code, which is good, but you don't state what the problem with the code is, exactly. Please do that. 2) about using this site in general. When your question is put on hold, you generally should not create duplicate questions, but rather keep improving the original question. When you do that, it will be reviewed and possibly reopened. – Lev Levitsky Jun 11 '14 at 22:00
  • Ok i am sorry. I did not know that it could be re opened. I am on the app so it is ristricted – user3729400 Jun 11 '14 at 22:07

2 Answers2

0

If you want to write the user's name to a file, you can simply do so in the else block of your code:

if os.path.isfile(external_file):
    with open(external_file) as in_f:
        name = in_f.read()
else:
    name = raw_input("What's your name?")
    with open(external_file, "w") as out_f:
        out_f.write(name)

I've also modified the code to use with statements to make sure the files get closed after you are done reading or writing them.

I'm not sure exactly how you want to allow the user to say that the name you read from the file is incorrect. The file writing part can use the same code as the else block above though.

Blckknght
  • 85,872
  • 10
  • 104
  • 150
  • It is a bot so you change per user so what would the change code be? – user3729400 Jun 11 '14 at 21:41
  • @user3729400: Your question doesn't even hint at how your code handles the differences between users, so I can't really answer that. You could use some kind of more sophisticated data structure (like a dictionary) that you write to the file (perhaps with `pickle`), or you could have several files, one per user. – Blckknght Jun 11 '14 at 21:49
0

This will append to the file and create the file if it does not exist:

import os
#hard code the path to the external file
external_file = 'names.txt'
#if file exists, use it to load name, else ask user
if not os.path.isfile(external_file):
    with open(external_file, "a") as f:
        pass
#if file exists, use it to load name, else ask user
name = raw_input("What's your name?")
with open(external_file, "r") as f:
    lines = f.read().strip().split() # split the names to avoid issues like name "a" in name "al" being True
    if name in lines:
        print "Hi {}".format(name)
    else:
        with open(external_file, "a") as f: # using "a" will append to the file
            f.write(name)
            f.write("\n")

I am not sure on your logic for checking if a user exists as you would need to know the name to check if the users name is already in the file.

This reflects your comments:

import os

#hard code the path to the external file
external_file = 'names.txt'
#if file does not exist ask user for name and save it
if not os.path.isfile(external_file):
    name = raw_input("What's your name?")
    print "Hi {} I am storing your name".format(name)
    with open(external_file, "a") as f:
        f.write(name)
else:
# file exists, use it to load name and print
    with open(external_file, "r") as f:
        name =f.read()
        print "Hi {}".format(name)

If the file does not exist, the name cannot be in there so this will ask the user for their name or print "Hi name" if the file does exist

Padraic Cunningham
  • 160,756
  • 20
  • 201
  • 286