A bug with file.tell()?

Nick Jacobson nicksjacobson at yahoo.com
Fri Sep 10 07:44:23 EDT 2004


It gets weirder:

I added the second fp.read(3) statement below.  And that statement
doesn't print anything out!  It (fp.read(3)) thinks it's at the end of
the file, while the next statement "for line in fp" thinks it's not
(and reads "asdf")!

Now I'm definitely confused.



fp = file("testcrlf.txt")
#read in some of the file:

print fp.read(3)
for line in fp:
    if line == "blah3\n":
        break
fpos = fp.tell() #save the current position...

print fp.read(3)
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!



More information about the Python-list mailing list