Try
fin = open('input.txt', 'r')
fout = open('output.txt', 'w')
linesin = fin.read().splitlines()
seqnum = 1
previous = None
for lin in linesin:
if previous != None:
if lin == 'APPLE':
previous += str(seqnum)
seqnum = seqnum + 1
fout.write(previous+'\n')
previous = lin
fout.write(previous+'\n')
fout.close()
fin.close()