Hi Mark, I googled a bit, and found http://bugs.python.org/issue1355023. It seems that this patch implemented fuller seek() for GzipFile around November 2006 (including whence==2). Do I misunderstand and this patch was not actually implemented, namely, is seek(-n,2) not implemented in Python 2.5.2 ?
The source of gzip.py on my system seems to suggest that negative seeks are supported: def seek(self, offset): if self.mode == WRITE: if offset < self.offset: raise IOError('Negative seek in write mode') count = offset - self.offset for i in range(count // 1024): self.write(1024 * '\0') self.write((count % 1024) * '\0') elif self.mode == READ: if offset < self.offset: # for negative seek, rewind and do positive seek self.rewind() count = offset - self.offset for i in range(count // 1024): self.read(1024) self.read(count % 1024) Could any who're familiar with the inner working of gzip settle this issue ? Is seek from EOF supported for gzip files ? Thanks, Ron. P.S.: as you guessed, my python 2.5.2 gives the not very helpful error: Traceback (most recent call last): File "./_LogStream.py", line 53, 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) -----Original Message----- From: Mark Tolonen [mailto:metolone+gm...@gmail.com] Sent: Thursday, January 08, 2009 17:16 To: python-list@python.org Subject: Re: Nubie question: how to not pass "self" in call to seek() function ? "Barak, Ron" <ron.ba...@lsi.com> wrote in message news:7f0503cd69378f49be0dc30661c6ccf602494...@enbmail01.lsi.com... > 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 ? Perhaps the extra agrument is 2. You don't mention your Python version, but in Python 2.6.1 gzip files don't support seeking from the end. An older (your?) version of Python may not be providing as helpful an error message. >>> f=gzip.GzipFile('blah.gz','r') >>> f.seek(-1,2) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\dev\python\lib\gzip.py", line 368, in seek raise ValueError('Seek from end not supported') ValueError: Seek from end not supported -Mark > > 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() -- http://mail.python.org/mailman/listinfo/python-list
-- http://mail.python.org/mailman/listinfo/python-list