Scriptr
11-01-2012, 01:41 PM
Intro:
I am a procrastinator. I start to do work and end up on facebook or otherwise.
There exists an app for OS X, Self-Control. It uses the hosts file to redirect requests to localhost.
However, such an app is not near the surface of google (and if it is, don't tell me. I'm nearly done writing it) for windows.
Meat of the Post:
I have a File IO class that writes and "unwrites" to the host file:
def write_to_hosts(ip_string, hosts):
f = open(hosts, "a")
f.write(ip_string)
f.close()
def un_write(ip_string, hosts):
print "IP list: \n", ip_string
f = open(hosts, "r")
h_text = f.read()
f.close()
print "hosts file:\n"+h_text
h_text.replace(ip_string, "")
print "hosts file:\n"+h_text
f = open(hosts, "w")
f.write(h_text)
f.close()
The problem lies in the un_write(ip_string, hosts). It doesn't un-write. (note, the print statements are only for debugging purposes). It writes it back exactly how it was.
What am I doing wrong here?
I am a procrastinator. I start to do work and end up on facebook or otherwise.
There exists an app for OS X, Self-Control. It uses the hosts file to redirect requests to localhost.
However, such an app is not near the surface of google (and if it is, don't tell me. I'm nearly done writing it) for windows.
Meat of the Post:
I have a File IO class that writes and "unwrites" to the host file:
def write_to_hosts(ip_string, hosts):
f = open(hosts, "a")
f.write(ip_string)
f.close()
def un_write(ip_string, hosts):
print "IP list: \n", ip_string
f = open(hosts, "r")
h_text = f.read()
f.close()
print "hosts file:\n"+h_text
h_text.replace(ip_string, "")
print "hosts file:\n"+h_text
f = open(hosts, "w")
f.write(h_text)
f.close()
The problem lies in the un_write(ip_string, hosts). It doesn't un-write. (note, the print statements are only for debugging purposes). It writes it back exactly how it was.
What am I doing wrong here?