I'm a newbie to python.

After opening a file in 'r' mode then using the approach below.

f = open( '/g.txt','r')
for line in f:

I find a pattern I'm looking for in a text file. Then I use f.tell() to record the file position. However, I think due to buffering the file position returned isn't correct, I think it must be the position of the end of the buffer that was in the caching, not the actual line I just read.

What am I doing wrong ?

thank you


Yes, "for line in f:" is buffered, meaning it reads more than one record at a time into a buffer. Use readline instead

fp=open(fname, 'r')
data_in = fp.readline()
while data_in: 
   print data_in,     ## assumes "\n" in record