elkclone

primitive file reader and search template

Feb 17th, 2014
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.01 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 with Rose and text as arguments for testing. Next step
  8. is to prompt user for the search string pattern argument for Find()"""
  9.  
  10. import sys
  11. import re
  12. def cat(filename):
  13.     f = open(filename, 'rU')
  14.     text = f.read()
  15.     print '----------------->your file has been opened..'
  16.     print text,
  17.     print '----------------->searching pattern Roses'
  18.     def Find(pat, text):
  19.         match = re.search(pat, text)
  20.         if match:
  21.             print match.group()
  22.         else:
  23.             print 'not found'
  24.     Find('Rose', 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