Advertisement
gruntfutuk

find_words_in_files

Jun 11th, 2018
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.83 KB | None | 0 0
  1. import os
  2. from pathlib import Path
  3.  
  4. def ReqET(liste, lpath):
  5.     ''' return list of files containing any of words in liste
  6.        and print contents of files in target folder in lpath
  7.    '''
  8.    
  9.     ldocs = os.listdir(lpath)
  10.     ld = []
  11.    
  12.     for doc in ldocs:
  13.         next_file = Path(lpath + "/" + doc)
  14.         if next_file.is_file():
  15.             with open(next_file,"r") as f:
  16.                 content = f.read()
  17.                 contents = content.split()
  18.                 print(f'file: {doc},\n {contents}')
  19.    
  20.             for search_string in liste:
  21.                 if search_string in contents:
  22.                     ld = ld + doc.split()
  23.  
  24.     return ld
  25.  
  26. liste = ['words','to', 'search', 'for']  # replace with words to find
  27. lpath = "/path/to/target/folder"  # replace with correct path
  28. print(ReqET(liste,lpath))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement