Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.76 KB | None | 0 0
  1. #! python3
  2. # regex_search.py - opens all .txt files in a folder and searches for any line
  3. # that matches a user-supplied regex.
  4.  
  5. import os, re
  6.  
  7. path = 'C:\\skrypty\\text_folder'
  8. os.chdir(path)
  9. texts = []
  10. text_string = ''
  11.  
  12. user_input =input("Enter a regex:\n")
  13. user_regex = re.compile(r'.*{0}.*'.format(user_input))
  14.  
  15. for file in range(len(os.listdir(path))):
  16.     if os.path.basename(os.listdir(path)[file]).endswith('.txt'):
  17.         open_file = open(os.listdir(path)[file])
  18.         read_file = open_file.read()
  19.         #texts += read_file
  20.         texts.append(read_file)
  21.         open_file.close()
  22.        
  23. text_string = '\n'.join(texts)
  24.  
  25. for result in range(len(user_regex.findall(text_string))):
  26.     print((user_regex.findall(text_string))[result])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement