Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: Un-write

  1. #1
    Regular Coder
    Join Date
    Oct 2011
    Posts
    106
    Thanks
    12
    Thanked 0 Times in 0 Posts

    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?

  • #2
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    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

  • #3
    Regular Coder
    Join Date
    Oct 2011
    Posts
    106
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by patryk View Post
    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.
    Last edited by Scriptr; 11-01-2012 at 05:46 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •