Go Back   CodingForums.com > :: Server side development > Other server side languages/ issues > Python

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-01-2012, 01:41 PM   PM User | #1
Scriptr
Regular Coder

 
Join Date: Oct 2011
Posts: 106
Thanks: 12
Thanked 0 Times in 0 Posts
Scriptr is an unknown quantity at this point
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?
Scriptr is offline   Reply With Quote
Old 11-01-2012, 02:29 PM   PM User | #2
patryk
Regular Coder

 
patryk's Avatar
 
Join Date: Oct 2012
Location: /dev/couch
Posts: 395
Thanks: 2
Thanked 64 Times in 64 Posts
patryk is on a distinguished road
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
patryk is offline   Reply With Quote
Old 11-01-2012, 04:35 PM   PM User | #3
Scriptr
Regular Coder

 
Join Date: Oct 2011
Posts: 106
Thanks: 12
Thanked 0 Times in 0 Posts
Scriptr is an unknown quantity at this point
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 04:46 PM..
Scriptr is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:56 AM.


Advertisement
Log in to turn off these ads.