View Single Post
Old May 2nd, 2008, 3:18 PM   #3
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
Re: Help with strings

It gets even simpler than that. You can use the "in" operator to test for the occurrence of a string within another string. For example, the following program will search for the string "quey" within every file inside all directories and subdirectories of "fdir".

Python Syntax (Toggle Plain Text)
  1. import os
  2.  
  3. query = "localhost".lower()
  4. fdir = 'aaron_storage'
  5.  
  6. for root, dirs, files in os.walk(fdir):
  7. for file in files:
  8. fname = os.path.join(root, file)
  9. if query in open(fname, 'r').read().lower():
  10. print '\n' + fname
  11. print '.',
  12.  
  13. raw_input('Done')
Sane is offline   Reply With Quote