Mega Code Archive

 
Categories / Python / Database
 

Find a word in shelve file

import sys, shelve, linecache shIn = shelve.open('indexfiles') for word in sys.argv[1:]:     if not shIn.has_key(word):          sys.stderr.write('Word %r not found in index file\n' % word)          continue     places = shIn[word]     for fname, lineno in places:         print "Word %r occurs in line %s of file %s:" % (word,lineno,fname)         print linecache.getline(fname, lineno),