Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. import re
  2. import sys
  3. import os
  4.  
  5. logz = [fn for fn in os.listdir(r'my text file directory') if fn.endswith('.txt')]
  6.  
  7. err_occur = [] # The list where we will store results.
  8. pattern = re.compile(" error ", re.IGNORECASE)
  9.  
  10.  
  11. try: # Try to:
  12. with open ('logz', 'rt') as in_file: # open file for reading text.
  13. for linenum, line in enumerate(in_file):
  14. if pattern.search(line) != None:
  15. err_occur.append((linenum, line.rstrip('/n')))
  16. print("Line ", linenum, ": ", line, sep='')
  17.  
  18. import pathlib
  19.  
  20.  
  21. def main():
  22. for path in pathlib.Path('.').iterdir():
  23. if path.suffix.lower() == '.txt':
  24. with path.open() as file:
  25. for line, text in enumerate(file):
  26. if 'error' in text.lower():
  27. print(f'Line {line}: {text}')
  28.  
  29.  
  30. if __name__ == '__main__':
  31. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement