0

I was asked to write a program to find string "error" from a file and print matched lines in python.

  1. Will first open a file with read more
  2. i use fh.readlines and store it in a variable
  3. After this, will use for loop and iterate line by line. check for the string "error".print those lines if found.

I was asked to use pointers in python since assigning file content to a variable consumes time when logfile contains huge output.

I did research on python pointers. But not found anything useful. Could anyone help me out writing the above code using pointers instead of storing the whole content in a variable.

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • 1
    You must have misunderstood the assignment. There are no pointers in Python. – DYZ Mar 15 '18 at 05:51
  • You don't need to read and store the entire file's contents. You can loop over a file object as if it were a list or any other iterable. Simply loop through the file and check each line; if it contains `error` print it. – Burhan Khalid Mar 15 '18 at 05:53
  • Yes. I didnot find anything related to python pointers. I used below code for line in open('c182573.log','r').readlines(): if ('Executing' in line): print line But they asked me to use pointers. – sujatha rathod Mar 15 '18 at 06:04
  • Are you talking about a file read / write position pointer? Then just iterate over a file opened in text mode and it will make use of that and some optimization automatically. – Klaus D. Mar 15 '18 at 06:15

4 Answers4

0

There are no pointers in python. But something like pointer can be implemented, but for your case it's not required. Try Below Code

with open('test.txt') as f:
    content = f.readlines()
for i in content:
  if  "error" in i:
    print(i.strip())

Even if you want to understand Python variables as pointers go to this link

http://scottlobdell.me/2013/08/understanding-python-variables-as-pointers/

Jay Shankar Gupta
  • 5,918
  • 1
  • 10
  • 27
0

There are no pointers in python, although something like pointer can be implemented, but is not worth the efforts for your case.
As pointed out in the solution of this link,
Read large text files in Python, line by line without loading it in to memory .
You can use something like:
with open("log.txt") as infile: for line in infile: if "error" in line: print(line.strip()) .
The context managers will close the file automatically and it only reads one line at a time. When the next line is read, the previous one will be garbage collected unless you have stored a reference to it somewhere else.

  • Can you please help me out in understanding of python pointers. I mean you said, something like pointer can be implemented. Any example which implements something like pointer in python will help me to understand it better – sujatha rathod Mar 15 '18 at 06:10
  • http://scottlobdell.me/2013/08/understanding-python-variables-as-pointers/ . https://stackoverflow.com/questions/1145722/simulating-pointers-in-python . Check these out. But, as I said, there are no pointers in python, just the illusion of pointer. :) – Sagar Ruchandani Mar 15 '18 at 06:17
0

You can use a dictionary by using key-pair value. Just dump the log file into dictionary wherein the key would be words and value would be the line number. So if you search for string "error" you will get the line numbers they are present it and accordingly you can print them. Since searching in dictionary or hashtable is in constant time O(1) it will take less time. But yes storing might take time depends if you avoid collision.

Adi
  • 186
  • 9
0

I used below code instead of putting the data in a variable and then for loop.

for line in open('c182573.log','r').readlines(): if ('Executing' in line): print line

So there is no way that we can implement pointers or reference in python.

Thanks all