A bug with file.tell()?

Nick Jacobson nicksjacobson at yahoo.com
Fri Sep 10 00:43:36 EDT 2004


Is this a bug?

This code involves the file methods "seek" and "tell".  Even though
the file pointer is in the middle of the file, "tell" returns a
position at the end of the file!

fp = file("test.txt")
#read in some of the file:
for line in fp:
    if line == "blah3\n":
        break
fpos = fp.tell() #save the current position...

for line in fp:
    print line #prints "asdf", so it wasn't at the end of the file

fp.seek(fpos) #rewind?
for line in fp:
    print line #prints nothing, because it's at the end of the file!


The test.txt file is:
blah1
blah2
blah3
asdf



More information about the Python-list mailing list