# L-17 MCS 260 Fri 19 Feb 2010 : locword # This program prints the locations in file # at each occurrent of the word 'the'. def add_to_buffer(b,c): """ Adds c to a 3-letter buffer b, on return is the updated buffer. """ B = b if len(b) == 3: (B[0],B[1],B[2]) = (B[1],B[2],c) else: B.append(c) return B search = ['t','h','e'] buffer = [] name = raw_input('give file name : ') file = open(name,'r') locations = [] while True: c = file.read(1) if c == '': break buffer = add_to_buffer(buffer,c) if buffer == search: locations.append(file.tell()-3) file.close() print 'the occurs at ', locations