file.readline() after a seek() breaking up lines

fd frnakd at aol.com
Fri Mar 5 15:05:34 EST 2004


I am a newcomer to python, and I hope someone can point out to me why
my calls to file.readline() (after a seek) are returning mangled lines. 
Calling readline twice after each seek, eliminates the problem.  Is seek(), 
like next(), incompatible with readline()?  If so, how should I be doing do 
random access line reads?
Thanks
FD

# Sample code for readline() problem

# platform: windows xp 
# python version 2.3
# The source file is just a list of words - one word per line,
# saved as ANSI from notepad 


from string import rstrip
from random import randrange

words = file('C:\\swap\\english.txt', 'r')
words.seek(-1,2)
endAt = words.tell()
startAt = 1

for w in range(0, 50):
    words.seek(randrange( startAt, endAt ) , 0)
    #words.readline() #uncomment this and lines are intact
    print words.readline() 

words.close()



More information about the Python-list mailing list