1) I made some fixes to your pasted code in an edit. I personally don't like reading / writing to the same file at the same time (especially under Windows), so here's a suggested alternate version. I also forgot about raw_input versus input so used How do I use raw_input in Python 3's suggestion
try: input = raw_input
except NameError: pass
f1 = open('C:\\WINDOWS\\system32\\drivers\\etc\\hosts', 'r')
data = f1.read()
f1.close()
f2 = open('C:\\WINDOWS\\system32\\drivers\\etc\\hosts', 'w')
usrinput1 = str(input('Enter A name in quotes:'))
for line in data.split("\n"):
if line.find("localhost") != -1:
f2.write(line + " " + usrinput1 + "\n")
else:
f2.write(line + "\n")
f2.close()
2) Note that being logged in as a user with administrator privileges isn't sufficient. You need to do runas adminstrator cmd (start menu -> all programs -> accessories -> right click command prompt) and then run python from there to have your python program have the right privileges. (Or you could follow the suggestion in one of the comments on alternate ways to get the right privileges)
3) Not sure if you meant to do this, but you're probably better off adding your new hostname to the localhost entry versus replacing localhost (so that if you run the program multiple times, it will work on subsequent runs)