# Prompt for three values: input file, output file, search string. # Search for the string in the input file, write results to the # output file import sys def process_file(i_file, o_file, a_str): ''' if the a_str is in a line of i_file, add stars to the a_str in line, write it out with the line number to o_file''' line_count_int = 1 for line_str in i_file: if a_str in line_str: new_line_str = line_str.replace(a_str, '***'+a_str) print('Line {}: {}'.format(line_count_int, new_line_str),\ file=o_file) line_count_int += 1 try: in_file_str = input("File to search:") in_file = open(in_file_str, 'r', encoding='utf_8') except IOError: print('{} is a bad file name'.format(in_file_str)) sys.exit() out_file_str = input("File to write results to:") out_file = open(out_file_str, 'w') search_str = input("Search for what string:") process_file(in_file, out_file, search_str) in_file.close() out_file.close()