Python Fabric won't comment beginning of line

193 views Asked by At

Just trying to comment out a line in a text file in python fabric. Regex is for any @ sign that doesn't have ip1 or ip2 following. The pattern searches just fine and finds the line, except instead of printing "# + line" it prints "#@#@bad_Ip_here". So it should be :

#*.info;mail.none;stuff.none;cron.none                @bad_ip

but it prints

*.info;mail.none;stuff.none;cron.none                #@#@bad_ip

It was just printing the commented line twice, which is fine. I don't know what is wrong or what changed. I tried forcing it to insert at position 0 with

line = line[:0] + "#" + line[0:]

but that didn't work either.

def addcomments():
    ip1 = '10.31.xx.xxx'
    ip2 = '10.30.xx.xx'
    host = env.host_string
    conf = open('/home/myuser/verify_yslog_conf/%s/hi.txt' % host, 'r')
    f = conf.read()
    conf.close()
    comment = open('/home/myuser/verify_yslog_conf/%s/hi.txt' % host, 'w')
    for line in f:
        if re.search(r'(@(?!({0}|{1})))'.format(ip1, ip2), line):
            line = "#" + line
            comment.write(line) 
        comment.write(line)
    comment.close()

thanks

0

There are 0 answers