Asad, Like many projects, there may be many ways to do things BUT some rules do apply.
You can only read an open file ONCE unless you seek back to the beginning or reopen it. string = f3.read() string1 = f3.readlines() The first line reads the entire file into a single buffer. The second program line won't work as intended. The first consumed the entire file. Much of the rest is not organized well enough for me to understand what you want to do. I find it important for people to try some simple things like examining the values step by step. Had you typed print (string) print (string1) on a small sample file, you might have fixed that before continuing. Then each step along the way you could examine and verify it made sense up to that point. Try writing the outline of the logic of your program first in English or your native language as an algorithm. Then see what tools are needed. Look at a sample of the log you are evaluating and see what it takes to locate the lines you want and then to break out the parts you want to keep for further use. What I see looks like this: If you find one instance of the string "ERR1" Then You want to find ALL (nonoverlapping) regions consisting of an upper-case letter followed by two lower-case letters and a space and either a space or digits 1 to 3 and digits 0-9 and a space and ... Fairly complex pattern. But you are searching the contents of the ENTIRE file for this and since you seem to have wanted to replace all newlines by spaces and your pattern includes spaces, this would match something that wrapped around from line to line. Is this what you wanted? You then switch gears to using the readlines version and I decided to get back to my regularly scheduled life. As noted, that probably is an empty string or worse. Good luck. -----Original Message----- From: Tutor <tutor-bounces+avigross=verizon....@python.org> On Behalf Of Asad Sent: Sunday, November 11, 2018 8:54 PM To: tutor@python.org Subject: Re: [Tutor] Example for read and readlines() (Asad) Hi All , Thanks for the reply . I am building a framework for the two error conditions, therefore I need to read and readlines because in one only regex is required and in other regex+ n-1 line is required to process : #Here we are opening the file and substituting space " " for each \n encountered f3 = open (r"D:\QI\log.log", 'r') string = f3.read() string1 = f3.readlines() regex = re.compile ( "\n" ) st = regex.sub ( " ", string ) if re.search('ERR1',st): y=re.findall("[A-Z][a-z][a-z] [ 123][0-9] [012][0-9]:[0-5][0-9]:[0-5][0-9] [0-9][0-9][0-9][0-9]",st) print y patchnumber = re.compile(r'(\d+)\/(\d+)') ======> doesnot work it only works if I use #string = f3.read() for j in range(len(string1)): if re.search ( r'ERR2', string1[j] ): print "Error line \n", string1[j - 1] mo = patchnumber.search (string1[j-1]) a = mo.group() print a print os.getcwd() break Please advice how to proceed. Thanks, _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor