In: Computer Science
Write a function redact() takes as input a file name. See secret.txt for the initial test. The function should print the contents of the file on the screen with this modification: Every occurrence of string 'secret' in the file should be replaced with string 'xxxxxx'. Every occurrence of “agent’ should be replaced with ‘yyyyyy’. Every occurrence of carrier should be replaced with ‘zzzzzz’. This function should catch exceptions.
FOR PYTHON
Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions. Thank You! =========================================================================== def redact(filename): try: with open(filename, 'r') as infile: for line in infile.readlines(): line = line.strip() for word in line.split(): word_c = word.lower() if 'secret' in word_c: word = word_c.replace('secret','xxxxxx') elif 'agent' in word_c: word = word_c.replace('agent', 'yyyyyy') elif 'carrier' in word_c: word = word_c.replace('carrier', 'zzzzzz') print(word, end=' ') print() except: print('Error: File {} not found'.format(filename)) redact('secrets.txt')
====================================================================