This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: file.tell() gives wrong result
Type: Stage:
Components: None Versions: Python 2.5
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: QuantumTim, yavuz164
Priority: normal Keywords:

Created on 2008-12-11 15:31 by yavuz164, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (4)
msg77617 - (view) Author: Yavuz Onder (yavuz164) Date: 2008-12-11 15:31
I find that file.tell returns not the byte offset of the next byte, but
possibly the byte offset of the next block to be read. I find it always
to be a multiple of 1024. Following is a demo of the bug. where I read a
 few lines into a text file, step back by the length of the last read
line, read again, and do not find the same data. What is returned is the
tail part of a line way down  in the file. I woeked around by keeping
track of the file pointer, and seek worked fine. tell() is at fault.
----------demonstration on a text file
Python 2.5.1 (r251:54863, Nov 14 2007, 16:00:54) 
[GCC 3.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> jf=open('junk','r')
>>> line=jf.next()
>>> line
'\n'
>>> line=jf.next()
>>> line
'See COPYING file in the same directory as this one for license.\n'
>>> line=jf.next()
>>> line
'\n'
>>> line=jf.next()
>>> line
'Thank you for trying this utility. I had great fun putting it\n'
>>> line=jf.next()
>>> line
'together, and I hope users will find it useful.\n'
>>> jf.seek(len(line),1)
>>> line=jf.next()
>>> line
'gle says Maps will not work without it. file:// URLs are \n'
msg77619 - (view) Author: Tim Gordon (QuantumTim) Date: 2008-12-11 15:52
See the documentation for file.next 
(http://docs.python.org/library/stdtypes.html#file.next).  As you can 
see, file.next uses a buffer which will mess with the result of other 
methods, such as file.tell.
msg77620 - (view) Author: Yavuz Onder (yavuz164) Date: 2008-12-11 16:10
O.K. But, is there a way to backtrack to the beginning of the last read
line, without explicitly implementing accounting of the real file
pointer in the code, and doing "seek(<file pointer>,0)"? 

The documentation you pointed to implies that, in "for name in file:"
loop a clean backtracking is not possible. Am I missing something?

Thanks.
msg77621 - (view) Author: Tim Gordon (QuantumTim) Date: 2008-12-11 16:13
Try using the readline method instead of next.  I don't think that 
applies the same buffering.
History
Date User Action Args
2022-04-11 14:56:42adminsetgithub: 48883
2008-12-11 16:13:52QuantumTimsetmessages: + msg77621
2008-12-11 16:10:56yavuz164setmessages: + msg77620
2008-12-11 16:02:15benjamin.petersonsetstatus: open -> closed
resolution: wont fix
2008-12-11 15:52:53QuantumTimsetnosy: + QuantumTim
messages: + msg77619
2008-12-11 15:31:16yavuz164create