In the book – Core Python Programming, there is following example –
>>> f = open('/tmp/x', 'w+')
>>> f.tell()
0
>>> f.write('test line 1\n') # add 12-char string [0-11]
>>> f.tell()
12
>>> f.write('test line 2\n') # add 12-char string [12-23]
>>> f.tell() # tell us current file location (end))
24
When I run the same code in my interpreter, I get 13L in place of 12 and 26L in place of 24.
I am running python 2.5 on Windows.
Has anything changed regarding behaviour or tell() in versions? How does tell decide the position in the file.
Thanks and Regards
Your file is opened in text mode. In that mode Python on Windows makes a translation between Windows line-endings and Unix line endings. On Windows a line ending is two characters while on Unix it is one (
'\n'), hence your result is expected.If you open the file in binary mode, you don’t get these translations.
And you would get 12 and 24 back from
tell()as well.