def regex_search(): while True: # prompt = 'Please provide the path to the folder which contains the .txt files that you wish to search in:\n' # input_file_path = pyip.inputStr(prompt=prompt) input_file_path = r'C:' path = Path(input_file_path) if path.is_dir(): break else: print( 'The given path either did not exist, or the path was not a folder.' ) regex_string_prompt = 'Please provide a regular expression to use to search the .txt files\n' regex_str = pyip.inputRegexStr(prompt=regex_string_prompt) for textFilePathObj in path.glob('*.txt'): file = open(textFilePathObj) user_regex = re.compile(regex_str) print( f'The following lines in {textFilePathObj.name} matched the user provided regex of {regex_str} :' ) for line in file.readlines(): if len(user_regex.findall(line)) > 0: print(line)
def regexSearch(): # Ask user for his expression expression = pyip.inputRegexStr("You want to search for: ") # Set counter for number of occurances expressionCounter = 0 # Store all .txt files in a list directory = Path.cwd() allFiles = (list(directory.glob("*.txt"))) for file in allFiles: # Open all .txt files in the folder currentFile = open(file, "r") content = currentFile.read() currentFile.close() # Search user expression in the file for match in re.finditer(expression, content): expressionCounter += 1 print("The expression occured " + str(expressionCounter) + " times")
""" A program that opens all .txt files in a folder and searches for any line that matches a user-supplied regular expression, and print to the screen. """ import os, glob, re, pyinputplus as pyip current_folder = os.getcwd() os.chdir(current_folder) for file in glob.glob("*.txt"): txt_obj = open(file, 'r') txt = txt_obj.read() user_supplied_regex_pattern = pyip.inputRegexStr( prompt=f'For file {file}: input the search regex pattern: ') regex = re.compile(user_supplied_regex_pattern) search_result = regex.findall(txt) # Print out result if search_result is not None: print(f"In file {file}: Found match(es): ") for i in range(len(search_result)): print(search_result[i] + " ") else: print("Not match found.") txt_obj.close()
''' Chapter 9 Reading and Writing Files Regex Search Write a program that opens all .txt files in a folder and searches for any line that matches a user-supplied regular expression. The results should be printed to the screen. ''' from pathlib import Path import pyinputplus as pyip import re # TODO: Bug in pyip??? inputRegexStr calls validateRegex instead of validateRegexStr regex = pyip.inputRegexStr('Please input a regex: ') pattern = re.compile(regex) p = Path.cwd() for txtFile in list(p.glob('*.txt')): with open(txtFile) as curFile: txtLines = curFile.readlines() for txtLine in txtLines: if (pattern.search(txtLine)): print(txtLine)
import pyinputplus as pyip from pathlib import Path import re def get_dir(count=0): dir = pyip.inputFilepath(prompt='Enter a directory: ') if (Path(dir).is_dir()): return dir else: if (count > 5): raise Exception("Too many tries") get_dir(count + 1) try: dir = Path(get_dir()) except: print('Too many tries') exit() regex = pyip.inputRegexStr(prompt='Enter the Regular Expression: ') r = re.compile(regex) files = list(dir.glob('*.txt')) for file in files: file_handle = open(file) contents = file_handle.read() print(r.findall(contents))
#!/usr/bin/env python3 # regex_search.py opens all .txt files in a folder and searches for any line that matches a user-supplied regular expression # The results are printed to the screen from pathlib import Path import re, os import pyinputplus as pyip # Prompt user to input a path to be searched file_path = input("Please enter a path to the folder you want to search:\n") file_path_obj = Path(file_path) # Now we have the file path as a string and Path object if Path.is_dir(file_path_obj) == True: # Check for valid directory # Prompt user to input a regular expression search_criteria = pyip.inputRegexStr( prompt="Enter a regular expression to search for:\n" ) text_files = os.listdir(file_path_obj) # List all files in dir for text_file in text_files: # Loop through all files try: # This will prevent the program from crashing if it cannot open one of the files if text_file.endswith(".txt"): # Find the text files contents = open(file_path_obj / text_file) # Open the file list_of_lines = contents.readlines() # Make a list of all the lines contents.close() # Close the text file for ( line ) in list_of_lines: # Loop through every item in the list of lines if ( search_criteria.search(line) != None ): # If the regex str finds a match print(
from pathlib import Path import re, os import pyinputplus as pyip #use glob to find txt files p = Path(r'H:\The Folder of many text files') p.glob('*.txt') txtFiles = list(p.glob('*.txt')) regExUserInput = pyip.inputRegexStr( prompt='Please provide a regex to search for: ') print(regExUserInput) regExUser = regExUserInput for fileN in txtFiles: curFile = open(fileN) curFileLines = list(curFile.readlines()) for line in range(len(curFileLines)): if regExUser.search(curFileLines[line]) != None: print('Match found in line ' + str(line) + '.\nIn file: ' + os.path.basename(fileN) + '. \nLine: ' + curFileLines[line]) print()