python - why is the regex function always popping up the Attribute Error? -


i have been writing function in python ip of computer. code given below :

    def getip(self):              self.path = "/root"             self.iplist = []             os.chdir(self.path)             os.system("ifconfig > ipfinder")             try:                      file = open("ipfinder","r")                     self.pattern = '(\d{1,3}\.){3}\d{1,3}'                     while true:                             line = file.readline()                             try:                                      ip = re.search(self.pattern, line).group()                                     self.iplist.append(ip)                             except attributeerror:                                     pass                      file.close()              except  eoferror:                     ip in self.iplist:                             print ip 

i know not way ip of machine. problem attributeerror pops every single time. why happening? why can't match found?

i ran in local. found 4 things be modified!

a) regex:- \d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}

b) trim space while reading:- file.readline().strip()

c) if comes end of line, break while:-

if line == '':     break 

d) instead of re.search, re.finall

the modified code works in system without attributeerror is:-

def getip(self):

    self.path = "/root"     self.iplist = []     os.chdir(self.path)     os.system("ifconfig > ipfinder")     try:              file = open("ipfinder","r")             self.pattern = '\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}'             while true:                     line = file.readline().strip()                     if line == '':                         break                     try:                              ip = re.findall(self.pattern, line)                             self.iplist.append(ip)                     except attributeerror:                             pass              file.close()      except  eoferror:             ip in self.iplist:                     print ip 

Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -