seek() returns unexpected results

Barak, Ron Ron.Barak at lsi.com
Thu Dec 25 08:04:36 EST 2008


 Hi,

When using seek() with a certain text file, I get strange results.
I looked at the text file with a binary editor and verified with 'file', and it's an ASCII English text file.

Running the script on other text files gives the expected output.

Could you suggest the reason ?

In the outputs below, when using generator.py as input for generator.py the 'diff' is a nice progression as expected. ('diff' is the difference between the file pointer location and the number of characters read up to now).

However, when using sac.log as input for generator.py the 'diff' is a strange series: 1, 2, 1, 2, 3, 4, 5, 6

Running generator.py on a bigger portion of sac.log produces these 'diff's:
 1, 2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Thanks,
Ron.

________________________________

$ cat -n generator.py
     1  #!/usr/bin/env python
     2
     3  import gzip
     4  import sys
     5  from Debug import _line as line
     6
     7  class LogStream():
     8
     9      def __init__(self, filename):
    10          self.filename = filename
    11          self.input_file = self.open_file(filename)
    12
    13      def open_file(self, in_file):
    14          try:
    15              f = gzip.GzipFile(in_file, "r")
    16              f.readline()
    17          except IOError:
    18              f = open(in_file, "r")
    19              f.readline()
    20          f.seek(0)
    21          return(f)
    22
    23      def line_generator(self):
    24          while True:
    25              line_ = self.input_file.readline()
    26              print line()+". line_==|"+line_.strip()+"|"
    27              self.total_chars += len(line_)
    28              print line()+". line length:",len(line_),"; file pointer:",self.input_file.tell(),
    29              print "; total chars:",self.total_chars,"; diff:",self.input_file.tell() - self.total_chars
    30              if not line_:
    31                  break
    32              yield line_.strip()
    33
    34
    35  if __name__ == "__main__":
    36
    37      filename = sys.argv[1]
    38      log_stream = LogStream(filename)
    39      log_stream.input_file.seek(0)
    40      log_stream.total_chars = 0
    41      line_generator = log_stream.line_generator()
    42      for i in range(8):
    43          line_ = line_generator.next()

$ python generator.py generator.py
26. line_==|#!/usr/bin/env python|
28. line length: 22 ; file pointer: 23 ; total chars: 22 ; diff: 1
26. line_==||
28. line length: 1 ; file pointer: 25 ; total chars: 23 ; diff: 2
26. line_==|import gzip|
28. line length: 12 ; file pointer: 38 ; total chars: 35 ; diff: 3
26. line_==|import sys|
28. line length: 11 ; file pointer: 50 ; total chars: 46 ; diff: 4
26. line_==|from Debug import _line as line|
28. line length: 32 ; file pointer: 83 ; total chars: 78 ; diff: 5
26. line_==||
28. line length: 1 ; file pointer: 85 ; total chars: 79 ; diff: 6
26. line_==|class LogStream():|
28. line length: 19 ; file pointer: 105 ; total chars: 98 ; diff: 7
26. line_==||
28. line length: 5 ; file pointer: 111 ; total chars: 103 ; diff: 8

$ python generator.py sac.log
26. line_==|Dec 15 15:45:20 iSW-00-090 svm_sac[1714]: AGENT[0]:  [tv_expand               ] tv_expand:........[Domain 5:DVol1_3CG1-MM-1229347528T0] maxVolCapacityMB:....[5121] tempVolCapacityMB:...[512] fivePercentMB:.......[256] expandedTempVolCap:..[1024]|
28. line length: 246 ; file pointer: 247 ; total chars: 246 ; diff: 1
26. line_==|Dec 15 15:45:20 iSW-00-090 svm_sac[1714]: AGENT[0]:  [daemon_send_expand_reque] [Domain 5:DVol1_3CG1-MM-1229347528T0]: Add expand request up to 1024 MB (2097152 sectors)|
28. line length: 170 ; file pointer: 418 ; total chars: 416 ; diff: 2
26. line_==|Dec 15 15:45:20 iSW-00-090 svm_sac[1746]: AGENT[0]:  [domain_expand_exec      ] [Domain 5:DVol1_3CG1-MM-1229347528T0]: Send expand up to 1024 MB (2097152 sectors): success|
28. line length: 172 ; file pointer: 589 ; total chars: 588 ; diff: 1
26. line_==|Dec 15 15:45:21 iSW-00-090 svm_sac[1744]: AGENT[0]:  [domain_poll             ] Domain [Domain 5]: SVM reported volume manager setup changed|
28. line length: 141 ; file pointer: 731 ; total chars: 729 ; diff: 2
26. line_==|Dec 15 15:45:21 iSW-00-090 svm_sac[1744]: AGENT[0]:  [domain_poll             ] Domain [Domain 5]: SVM reported multiview setup changed|
28. line length: 136 ; file pointer: 868 ; total chars: 865 ; diff: 3
26. line_==|Dec 15 15:45:21 iSW-00-090 svm_sac[1744]: AGENT[0]:  [domain_is_setup_changed ] Domain [Domain 5]: Setup CHANGED|
28. line length: 113 ; file pointer: 982 ; total chars: 978 ; diff: 4
26. line_==|Dec 15 15:45:21 iSW-00-090 svm_sac[1744]: AGENT[0]:  [domain_get_pages        ] Domain [Domain 5]: Asked to get setup pages|
28. line length: 124 ; file pointer: 1107 ; total chars: 1102 ; diff: 5
26. line_==|Dec 15 15:45:21 iSW-00-090 svm_sac[1744]: AGENT[0]:  [svm_get_pages           ] SVM [Domain 5:12345]: got all setup pages|
28. line length: 122 ; file pointer: 1230 ; total chars: 1224 ; diff: 6

$ cat generator.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)

    def open_file(self, in_file):
        try:
            f = gzip.GzipFile(in_file, "r")
            f.readline()
        except IOError:
            f = open(in_file, "r")
            f.readline()
        f.seek(0)
        return(f)

    def line_generator(self):
        while True:
            line_ = self.input_file.readline()
            print line()+". line_==|"+line_.strip()+"|"
            self.total_chars += len(line_)
            print line()+". line length:",len(line_),"; file pointer:",self.input_file.tell(),
            print "; total chars:",self.total_chars,"; diff:",self.input_file.tell() - self.total_chars
            if not line_:
                break
            yield line_.strip()

if __name__ == "__main__":

    filename = sys.argv[1]
    log_stream = LogStream(filename)
    log_stream.input_file.seek(0)
    log_stream.total_chars = 0
    line_generator = log_stream.line_generator()
    for i in range(8):
        line_ = line_generator.next()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20081225/1596ac3f/attachment-0001.html>


More information about the Python-list mailing list