Nubie question: how to not pass "self" in call to seek() function ?

Barak, Ron Ron.Barak at lsi.com
Thu Jan 8 15:11:20 CET 2009


Hi,

I am getting the error TypeError: seek() takes exactly 2 arguments (3 given), namely:

$ ./_LogStream.py
Traceback (most recent call last):
  File "./_LogStream.py", line 47, in <module>
    log_stream.last_line_loc_and_contents()
  File "./_LogStream.py", line 20, in last_line_loc_and_contents
    self.input_file.seek(-1, 2) # grab the last character
TypeError: seek() takes exactly 2 arguments (3 given)

When I run the below code.

I understand that the extra argument is the "self", but I don't know how to change my class to make the seek(-1,2) work.

Could you help ?

Thanks,
Ron.

$ cat _LogStream.py
#!/usr/bin/env python

import gzip
import sys

from Debug import _line as line

class LogStream():

    def __init__(self, filename):
        self.filename = filename
        self.input_file = self.open_file(filename)
        self.index_increment = 10
        self.last_line_offset = -1

    def last_line_loc_and_contents(self, estimated_line_size=1024):

        assert estimated_line_size > 0
        file_size = len(self.input_file.read())
        self.input_file.seek(-1, 2) # grab the last character

        if self.input_file.read(1) == '\n': # a "proper" text file
            file_size -= 1

    def open_file(self, in_file):
        """
        The gzip module checks if the input file is a gzipped file, only at the read stage.
        This is why the f.readline() is needed.
        """
        try:
            f = gzip.GzipFile(in_file, "r")
            f.readline()
        except IOError:
            f = open(in_file, "r")
            f.readline()

        f.seek(0)
        return(f)


if __name__ == "__main__":
    filename = "../save_state/hp/save_state-ssp8400-0709R00004_081126-121659/var/log\\sac.log.4.gz"
    log_stream = LogStream(filename)
    log_stream.limit_ = 1000
    log_stream.index_increment = 12

    log_stream.last_line_loc_and_contents()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090108/3cf9b861/attachment.html>


More information about the Python-list mailing list