file - Python stream.tell() returns incorrect position -


tried use tell() cursor position after reading line. seems has problem text mode , \r. misusing method or it's bug? thanks.

tested python2.6, 2.7, 3.3 , have same output.

import io  io.open('a', 'w') fo:     fo.write(u'abc\n')     fo.write(u'def\r\n')     fo.write(u'hij\r')     fo.write(u'klm')  print('*io.open rb*') io.open('a', 'rb') fo:     print('--%r--%d' % (fo.readline(), fo.tell()))     print('--%r--%d' % (fo.readline(), fo.tell()))     print('--%r--%d' % (fo.readline(), fo.tell()))     print('--%r--%d' % (fo.readline(), fo.tell()))   print('*io.open rt*') io.open('a', 'rt') fo:     print('--%r--%d' % (fo.readline(), fo.tell()))     print('--%r--%d' % (fo.readline(), fo.tell()))     print('--%r--%d' % (fo.readline(), fo.tell()))     print('--%r--%d' % (fo.readline(), fo.tell())) 

output: (not sure 340282367000166625996085689099021713421 is)

*io.open rb* --b'abc\n'--4 --b'def\r\n'--9 --b'hij\rklm'--16 --b''--16 *io.open rt* --'abc\n'--4 --'def\n'--9 --'hij\n'--340282367000166625996085689099021713421 --'klm'--16 

there 3 types of eol (end of line) in text files:

  • windows: \r\n
  • unix (linux): \n
  • mac: \r

you have use correct eol correct format.

  • try create unix format file: io.open('a', 'w'),
  • or create unix format file: io.open('a', 'wb'),
  • and not use '\r' neither unix nor windows format:

windows format file:

            import io              io.open('a', 'w') fo: #this windows format file                 fo.write(u'abc\n')                 fo.write(u'def\n')                 fo.write(u'hij')                 fo.write(u'klm')               print('*io.open rb*')             io.open('a', 'rb') fo:                 print('--%r--%d' % (fo.readline(), fo.tell()))                 print('--%r--%d' % (fo.readline(), fo.tell()))                 print('--%r--%d' % (fo.readline(), fo.tell()))                 print('--%r--%d' % (fo.readline(), fo.tell()))              print('*io.open rt*')             io.open('a', 'rt') fo:                 print('--%r--%d' % (fo.readline(), fo.tell()))                 print('--%r--%d' % (fo.readline(), fo.tell()))                 print('--%r--%d' % (fo.readline(), fo.tell()))                 print('--%r--%d' % (fo.readline(), fo.tell()))              >>>              *io.open rb*             --'abc\r\n'--5             --'def\r\n'--10             --'hijklm'--16             --''--16              *io.open rt*             --u'abc\n'--5             --u'def\n'--10             --u'hijklm'--16             --u''--16             >>>              import io              io.open('a', 'wb') fo: #this unix format file                 fo.write(u'abc\n')                 fo.write(u'def\n')                 fo.write(u'hij')                 fo.write(u'klm')              print('*io.open rb*')             io.open('a', 'rb') fo:                 print('--%r--%d' % (fo.readline(), fo.tell()))                 print('--%r--%d' % (fo.readline(), fo.tell()))                 print('--%r--%d' % (fo.readline(), fo.tell()))                 print('--%r--%d' % (fo.readline(), fo.tell()))              print('*io.open rt*')             io.open('a', 'rt') fo:                 print('--%r--%d' % (fo.readline(), fo.tell()))                 print('--%r--%d' % (fo.readline(), fo.tell()))                 print('--%r--%d' % (fo.readline(), fo.tell()))                 print('--%r--%d' % (fo.readline(), fo.tell()))              >>>             *io.open rb*             --'abc\n'--4             --'def\n'--8             --'hijklm'--14             --''--14              *io.open rt*             --u'abc\n'--4             --u'def\n'--8             --u'hijklm'--14             --u''--14             >>> 

as can see results same, without strange long number


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -