file.write() in python
<pre><code>import os
import sys
import re
import string

f=open('./iprange','r')
s=f.readline()
f.close()
pattern='inet addr:'+s
pattern=pattern.split('x')[0]
pattern='('+pattern+'...'+')'

os.system('ifconfig -a &gt;&gt; interfaces')
f=open('./interfaces','r')
s=f.readline()

while (len(s))!=0:
    i=re.search(pattern,s)
    if i!=None:
        sp=re.split(pattern,s)[1]
        ip=re.split('inet addr:',sp)[1]
        break
    s=f.readline()

f.close()
os.system('rm ./interfaces')
f=open('./userip','w')
f.write(ip)
f.close()

NameError;name 'ip' is not defined
</code></pre>

<p>I split <code>pattern</code> by <code>s</code> and store the result in <code>sp</code>, then I find the IP address and store the result in <code>ip</code>. But the error says <code>ip</code> is not defined - what's going on?</p>
