1

I want to edit 'hosts' file in this path : C:\Windows\System32\Drivers\etc. I am using windows 8.

My code is this:

f1 = open('C:\\WINDOWS\\system32\\drivers\\etc\\hosts', 'r')
f2 = open('C:\\WINDOWS\\system32\\drivers\\etc\\hosts', 'w')
usrinput1 = str(input('Enter A name:'))
for line in f1:
   f2.write(line.replace('localhost', usrinput1))
f1.close()
f2.close()

I don't have enough permission to edit this file. I logged in with admin user. Why is my code incorrect?

Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100
InJecTable
  • 53
  • 1
  • 1
  • 7
  • 1
    When you say you don't have permission to edit the file, is that an error you get when you run it? Can you post that error in full? Also, is that your actual indentation? – thegrinner Sep 09 '13 at 13:51
  • 3
    would guess this is a GOOD security measure. – Joop Sep 09 '13 at 13:52
  • I fixed the indents for your for loop and also added extra slashes, which might be part of the problem as opposed to just part of code posting issues – Foon Sep 09 '13 at 13:52
  • If you need to invoke a UAC elevated request, either run the script with administrative privilege or refer the [SO Answer to Request UAC elevation from within a Python script](http://stackoverflow.com/a/11746382/977038) – Abhijit Sep 09 '13 at 13:56
  • this error occurs when I run the script : http://s4.picofile.com/file/7929496448/stack.png – InJecTable Sep 09 '13 at 13:58

1 Answers1

0

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)

Community
  • 1
  • 1
Foon
  • 6,148
  • 11
  • 40
  • 42
  • Not worked.And gave the same error.You mean there is no way to edit an operation file ? – InJecTable Sep 09 '13 at 14:03
  • @InJecTable Did you do the runas bit? I just tried it with my python (which is admittedly 2.X and it worked just fine after I fixed some other bugs in my code (naming input isn't what you want to use) – Foon Sep 09 '13 at 14:13
  • I said in python 3.x not 2.x ! – InJecTable Sep 09 '13 at 14:16
  • @InJetTable I know you did, which is why I said admittedly, but there isn't a good reason I'm aware of that should matter for this case – Foon Sep 09 '13 at 14:16
  • Other than input vs rawinput (fixed in latest edit), my code worked for me in Python 3.3 on Windows 7 when I used the runas bit in my 2nd "bullet" so that I invoked python from an Administrator cmd shell. – Foon Sep 09 '13 at 14:25
  • (And just to state the obvious, before running this or any other command that modifies system files (or files in general), you should be smarter than me and backup your system file first, in case a typo breaks it part of the way through and you find your file is half missing) – Foon Sep 09 '13 at 14:26
  • And no, I don't have a Windows 8 box to test, so that could be the issue. – Foon Sep 09 '13 at 14:27
  • 1
    `if line.find("localhost") != -1:` - you might want to use the `in` operator instead... – l4mpi Sep 09 '13 at 16:30
  • Thanks.It worked for me in updating /etc/hosts with new ip entries in ubuntu 14 – Muthukkumaran Oct 10 '15 at 15:47