The tell method in the for loop returns an error when the Python file is read

The

code iterates one line at a time, expecting the tell method to return the position of the current pointer. But what is actually returned is the last location of the entire document;
Please help me to see what the problem is?

My Daily.txt

Good morning:
    As we known, Chengdu IS one of largest cities in the world!
    Set up
    get up
    take off
OK

the code is as follows

with open(r"My Daily", "r+") as f:
    for line in f:
        print f.tell()
Mar.15,2021

when using for traversal on the file object f, the contents of f are read out and iterable objects are generated for traversal.

so your output is always at the end


for line in f:
is when the f.next ().
f.next () method is called, the f.tell () method cannot be called

if you need to return the current pointer position, you can do this
pos = 0
with open (r'My Daily', 'ringing') as f:

for line in f:
    pos += len(lien)
    -sharpprint f.tell()
    print(post)
Menu