Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: 'str' object has no attribute 'seek' with python

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))
like image 905
Rakesh Avatar asked Jan 26 '23 03:01

Rakesh


1 Answers

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
like image 75
oszkar Avatar answered Feb 05 '23 16:02

oszkar