python working with files as they are written
<p>So I'm trying to create a little script to deal with some logs. I'm just learning python, but know about loops and such in other languages. It seems that I don't understand quite how the loops work in python.  </p>

<p>I have a raw log from which I'm trying to isolate just the external IP addresses. An example line:</p>

<blockquote>
  <p>05/09/2011 17:00:18   192.168.111.26  192.168.111.255 Broadcast packet dropped    udp/netbios-ns  0   0   X0      0   0   N/A</p>
</blockquote>

<p>And heres the code I have so far:</p>

<pre><code>import os,glob,fileinput,re

def parseips():
    f = open("126logs.txt",'rb')
    r = open("rawips.txt",'r+',os.O_NONBLOCK)

    for line in f:
        rf = open("rawips.txt",'r+',os.O_NONBLOCK)
        ip = line.split()[3]
        res=re.search('192.168.',ip)
        if not res:
            rf.flush()
            for line2 in rf:
                if ip not in line2:
                    r.write(ip+'\n')
                    print 'else write'
                else:
                    print "no"
    f.close()
    r.close()
    rf.close()  

parseips()
</code></pre>

<p>I have it parsing out the external ip's just fine. But, thinking like a ninja, I thought how cool would it be to handle dupes? The idea or thought process was that I can check the file that the ips are being written to against the current line for a match, and if there is a match, don't write. But this produces many more times the dupes than before :)  I could probably use something else, but I'm liking python and it makes me look busy. </p>

<p>Thanks for any insider info.</p>
