A bug with file.tell()?

Elbert Lev elbertlev at hotmail.com
Sat Sep 11 00:42:42 EDT 2004


Here is the script. In the first part I open the file and read it line
by line.
In the second part I open the same file and read lines in: "for s in
f:". The difference is that the second method first reads the whoole
file in memory and creates the list of string. Actually you are taking
strings from this list.

f = file("test")
fo = file("out", "w")

while 1:
    off = f.tell()
    s = f.readline()
    if not s: break
    print >>fo, "%5d:%s" % (off, s)
    
f = file("test")
print  >>fo, "==========================="
for s in f:
    off = f.tell()
    print  >>fo, "%5d:%s" % (off, s)


Output:
    0:blah1
    7:blah2
   14:blah3
   21:asdf 
===========================
   25:blah1
   25:blah2
   25:blah3
   25:asdf

By the way, because for s if f: reads the whoole file in memory such
construct is not recommended for reading large files (but is very
convenient).

I do not think, this is a bug.



More information about the Python-list mailing list