Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! python3
- # regex_search.py - opens all .txt files in a folder and searches for any line
- # that matches a user-supplied regex.
- import os, re
- path = 'C:\\skrypty\\text_folder'
- os.chdir(path)
- texts = []
- text_string = ''
- user_input =input("Enter a regex:\n")
- user_regex = re.compile(r'.*{0}.*'.format(user_input))
- for file in range(len(os.listdir(path))):
- if os.path.basename(os.listdir(path)[file]).endswith('.txt'):
- open_file = open(os.listdir(path)[file])
- read_file = open_file.read()
- #texts += read_file
- texts.append(read_file)
- open_file.close()
- text_string = '\n'.join(texts)
- for result in range(len(user_regex.findall(text_string))):
- print((user_regex.findall(text_string))[result])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement