Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.80 KB | None | 0 0
  1. import os
  2. mypath = os.getcwd()
  3. patterns = ['trunkate', 'delete']
  4. found_string = list()
  5. all_files = list()
  6.  
  7. for dirpath, subdirs, files in os.walk(mypath):
  8.     all_files.extend(os.path.join(dirpath, x) for x in files)
  9. all_files.pop(0)
  10.  
  11.  
  12. for file in all_files:
  13.     with open(file, 'r') as open_file:
  14.         file_body = open_file.readlines()
  15.     for num_line, line_text in enumerate(file_body):
  16.         for pat in patterns:
  17.             if pat in line_text.lower():
  18.                 found_string.append((str(file), str(num_line), str(pat)))
  19. print("FilePath;NumberLine;Pattern")
  20. for i in found_string:
  21.     print(f"{i[0]};{i[1]};{i[2]}")
  22.  
  23. with open("output.txt", 'w') as file:
  24.     file.write("FilePath;NumberLine;Pattern\n")
  25.     for i in found_string:
  26.         file.write(f"{i[0]};{i[1]};{i[2]}\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement