Getting "AttributeError: 'str' object has no attribute 'seek' " while running the below code. Can someone point where the issue is?
import re
import os
import time
regex = ' \[GC \((?<jvmGcCause>.*?)\).+?(?<jvmGcRecycletime>\d+\.\d+) secs\]'
read_line = True
def follow(thefile):
    thefile.seek(0,os.SEEK_END)
    while True:
        lines = thefile.readline()
        if not lines:
            time.sleep(0.1)
            continue
        yield lines
if __name__ == '__main__':
    logfile = r"/gc.log"
    loglines = follow(logfile)
    for line in loglines:
        match = re.search(regex, line)
        if match:
            print('jvmGcCause: ' + +match.group(1))
            print('jvmGcRecycletime: ' + match.group(2))
                In python seek is a method of file object, and you are trying to apply it on a string. You have to open the file first, and call seek on the opened file object.
Do something like this:
def follow(file_name):
    with open filename as the_file:
        the_file.seek(0, os.SEEK_END)
        while True:
            lines = the_file.readline()
            if not lines:
                time.sleep(0.1)
                continue
            yield lines
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With