import fileinput, os, anydbm
wordPos = {}
sep = os.pathsep
for line in fileinput.input():
pos = '%s%s%s'%(fileinput.filename(), sep, fileinput.filelineno())
for word in line.split():
wordPos.setdefault(word,[]).append(pos)
dbmOut = anydbm.open('indexfile','n')
sep2 = sep * 2
for word in wordPos:
dbmOut[word] = sep2.join(wordPos[word])
dbmOut.close()