title: Python, The fastest way to find string in multiple text files (some files are big)
body: <p>I try to search a string in multiple files, my code works fine but for big text files it takes a few minutes.</p>
  
  <pre><code>wrd = b'my_word'
  path = 'C:\path\to\files'
      #### opens the path where all of .txt files are ####
  for f in os.listdir(path):
      if f.strip().endswith('.txt'):
          with open(os.path.join(path, f), 'rb') as ofile:
          #### loops through every line in the file comparing the strings ####
              for line in ofile:                
                 if wrd in line:
                  try:
                      sendMail(...)
                      logging.warning('There is an error {} in this file : {}'.format(line, f))
                      sys.exit(0)
                  except IOError as e:
                      logging.error('Operation failed: {}' .format(e.strerror))
                      sys.exit(0)
  </code></pre>
  
  <p>I found this topic : <a href="https://stackoverflow.com/questions/36572887/python-finds-a-string-in-multiple-files-recursively-and-returns-the-file-path/47557527#47557527">Python finds a string in multiple files recursively and returns the file path</a> 
  but it does not answer my question..</p>
  
  <p>Do you have an idea how to make it faster ?</p>
  
  <p>Am using python3.4 on windows server 2003.</p>
  
  <p>Thx ;)</p>