Logic lover Logic lover - 5 months ago 28
Python Question

Unexpected file pointer position using ftell() in python traversing through for loop

Getting unexpected output of the following code below:

sample.txt
contains:


this is the first line

this is the second line

this is the third line

this is the fourth line

this is the fifth line

this is the sixth line


code:

import sys

f1=open("sample3.txt",'r')

print f1.tell()

for line in f1:
print line
print "postion of the file pointer",f1.tell()


Output:

0
this is the first line
postion of the file pointer 141
this is the second line
postion of the file pointer 141
this is the third line
postion of the file pointer 141
this is the fourth line
postion of the file pointer 141
this is the fifth line
postion of the file pointer 141
this is the sixth line
postion of the file pointer 141


I expect something which shows the file pointer position the end of each line

Answer Source

tell() does not work when you iterate over a file object. Due to some optimizations for faster reads, the actual current potion in the file does not make sense once you start iterating.

Python 3 provides more help here:

OSError: telling position disabled by next() call

Using readline() works better:

from __future__ import print_function

f1 = open('sample3.txt')
line = f1.readline()
while line:
    print(line)
    print("postion of the file pointer", f1.tell() )
    line = f1.readline()