I have a file that contains a list of names, one name per line.
I want to check if a name already exists in the file. If it doesn't, I want to append it at the end of the file.
names.txt
Ben
Junha
Nigel
Here is what I tried to do:
name=raw_input(" Name: ")
with open("names.txt") as fhand:
if name in fhand:
print "The name has already been in there"
else:
with open("file.txt","a+") as fhand:
fhand.write(name)
but the existing names are never found, and the names I enter always get appended to the last line.