Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #prints to console and creates and writes to a seperate file.
- import re
- user_input = input("What would you like to search for?\n")
- err_occur = [] # The list where we will store results.
- pattern = re.compile(user_input, re.IGNORECASE) # Compile a case-insensitive regex pattern.
- try: # Try to:
- date_time = input("Date and time of the file?\n")
- with open ('output_log__{}.txt'.format(date_time), 'rt') as in_file: # open file for reading text.
- with open(user_input+'_'+date_time+'.txt', 'w') as wf: # Creates a file to write to using user input from above
- for linenum, line in enumerate(in_file): # Iterate on file per line, keeping track of line numbers.
- if pattern.search(line) != None: # If substring search finds a match,
- err_occur.append((linenum, line.rstrip('\n'))) # strip linebreaks, store line and line number in list as tuple.
- for linenum, line in err_occur: # Iterate over the list of tuples, and
- print("Line ", linenum, ": ", line, sep='') # print results as "Line [linenum]: [line]".
- wf.write(line+"\n")
- except FileNotFoundError: # If log file not found,
- print("Log file not found.") # print an error message.
- print("year-month-day__hour-minute-second")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement