CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Python (http://www.codingforums.com/forumdisplay.php?f=46)
-   -   Un-write (http://www.codingforums.com/showthread.php?t=280316)

Scriptr 11-01-2012 01:41 PM

Un-write
 
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:
Code:



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?

patryk 11-01-2012 02:29 PM

why would you write classes for that stuff?
check 'grep', 'sed' and 'echo' utilities. that's all you need.
you have sources for those. just recompile (i'm sure you'll finde ports) for your system and use them

Scriptr 11-01-2012 04:35 PM

Quote:

Originally Posted by patryk (Post 1287461)
why would you write classes for that stuff?
check 'grep', 'sed' and 'echo' utilities. that's all you need.
you have sources for those. just recompile (i'm sure you'll finde ports) for your system and use them

Not classes, so much as separate files.
I come from Java where the two are virtually synonymous, and the convention is one public class per file.

Also, I am aware of ports for shell commands, if that is what you are referring to, but I am not concerned about those. I am learning Python, which needn't be recompiled, and this is a simple project to help me learn as well as solve a practical problem.

No offense intended, but I would like an answer to the question asked, not a correction.


All times are GMT +1. The time now is 08:27 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.