2

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

Mike Müller
  • 82,630
  • 20
  • 166
  • 161
Chinna
  • 79
  • 7
  • 2
    Possible duplicate of [file.tell() inconsistency](https://stackoverflow.com/questions/14145082/file-tell-inconsistency) – Pim Jul 02 '17 at 11:53

2 Answers2

1

I found the relevant part in the documentation:

A file object is its own iterator, for example iter(f) returns f (unless f is closed). When a file is used as an iterator, typically in a for loop (for example, for line in f: print line.strip()), the next() method is called repeatedly. This method returns the next input line, or raises StopIteration when EOF is hit when the file is open for reading (behavior is undefined when the file is open for writing). In order to make a for loop the most efficient way of looping over the lines of a file (a very common operation), the next() method uses a hidden read-ahead buffer. As a consequence of using a read-ahead buffer, combining next() with other file methods (like readline()) does not work right. However, using seek() to reposition the file to an absolute position will flush the read-ahead buffer.

Mike Müller
  • 82,630
  • 20
  • 166
  • 161
Chinna
  • 79
  • 7
0

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()
Mike Müller
  • 82,630
  • 20
  • 166
  • 161
  • Thank you for the prompt response. Can you please eloborate why can't we iterate the file using for loop. – Chinna Jul 02 '17 at 12:16