elkclone

primitive file reader and text search template

Feb 17th, 2014
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.00 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. """good use of primitive built in functions to process whole files.
  4. invoked the script defines cat() takes a filename as argument opens
  5. and reads it as a single text file then Find() function is defined
  6. using regular expressions built in functions search() match() and group().
  7. Find() is called after a prompt to user for the search string pattern argument for Find()"""
  8.  
  9. import sys
  10. import re
  11. def cat(filename):
  12.     f = open(filename, 'rU')
  13.     text = f.read()
  14.     print '----------------->your file has been opened..'
  15.     print text,
  16.     print '----------------->loading search function..'
  17.     def Find(pat, text):
  18.         match = re.search(pat, text)
  19.         if match:
  20.             print match.group()
  21.         else:
  22.             print 'not found'
  23.     x = raw_input('enter search pattern-->')
  24.     Find(x, text)
  25.     f.close()
  26.  
  27. #main() to dump file to screen
  28. def main():
  29.     cat(sys.argv[1])
  30.  
  31. #boilerplate main() call.
  32. if __name__=='__main__':
  33.    main()
Advertisement
Add Comment
Please, Sign In to add comment