[Tutor] Example for read and readlines() (Asad)

Cameron Simpson cs at cskk.id.au
Tue Nov 13 05:17:17 EST 2018


On 12Nov2018 07:24, Asad <asad.hasan2004 at gmail.com> wrote:
>       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()

My first remark is that both these lines read _and_ _consume_ the file 
content. So "string" gets the entire file content, and "string1" gets an 
empty array of lines, because the file is already at the end, where 
there is no more data.

It is also better to use this idiom to read and then close a file:

  with open(r"D:\QI\log.log", 'r') as f3:
    string = f3.read()

This reliably closes f3 once the "with" suite completes, even if there's 
some kind of exception.

You need 2 copies of the file data. You can do this 2 ways. The first 
way is to read the file twice:

  with open(r"D:\QI\log.log", 'r') as f3:
    string = f3.read()
  with open(r"D:\QI\log.log", 'r') as f3:
    string1 = f3.readlines()

The efficient way is to read the file once, then make string from 
string1, or string1 from string. For example:

  with open(r"D:\QI\log.log", 'r') as f3:
    string1 = f3.readlines()
  string = ''.join(string1)

>regex = re.compile ( "\n" )
>st = regex.sub ( " ", string )

Using a regular expression to replace a fixed string such as "\n" is 
overkill. Consider:

  st = string.replace("\n", " ")

Python strings have a bunch of handy methods for common simple things.  
Have a read of the docs for further detail.

>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

On the other hand, a regexp is a good tool for something like the above.

>patchnumber = re.compile(r'(\d+)\/(\d+)')                ======> doesnot
>work it only works if I use  #string = f3.read()

This may be because "string" is a single string (the whole file text as 
one string). "string1" is a _list_ of individual strings, one for each 
line. Personally, i would call this "strings" or "lines" or some other 
plural word; your code will be easier to read, and easier to debug.

Conversely, a misleading name makes debugging harder because you expect 
the variable to contain what its name suggests, and if it doesn't this 
will impede you in finding problems, because you will be thinking the 
whrong thing about what your program is doing.

>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.

mo.group() returns the whole match. The above seems to look for the 
string 'ERR2' in a line, and look for a patch number in the previous 
line. Is that what is it supposed to do?

If the above isn't working, it would help to see the failing output and 
a description of what good output is meant to look like.

Finally, please consider turning off "digest mode" in your list 
subscription. It will make things easier for everyone.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Tutor mailing list