# Write an automated censor program that reads in the text from a file and creates a new file where all of the four letter words have been replaced by "****". You can ignore punctuation and you may assume that no words in the file are split across multiple lines. import os def Censor_Words(input_file, output_file): print(f"Censoring words in file: , {input_file} and writing them to {output_file}\n") #check if output file exists and if not then create it if not os.path.exists(output_file): open(output_file, 'w').close() else: print(f"{output_file} already exists. Overwriting it.\n") try: # try to open the file and write to the output file with open(input_file, 'r') as f: # open the file, using r to read read it text = f.read() # read the file words = text.split() # split up the words for i in range(len(words)): # loop through the words. Len(words) gives the number of words in the list if len(words[i]) == 4: # if the word is 4 characters, then replace it with **** print(f"Word {words[i]} censored.\n") #output the word that was censored for troubleshooting. words[i] = "****" with open(output_file, 'w') as f: # open the output file, using w to write to it f.write(" ".join(words)) print("Censoring document complete. Results found in output.txt\n") except FileNotFoundError: print("File wasn't found.") except: print("An error occurred.") def __main__(): script_directory = os.path.dirname(os.path.abspath(__file__)) # get the directory of the script input_file = os.path.join(script_directory, "input.txt") #had to use os.path.join to get the correct path. Wasn't working with just the file name output_file = os.path.join(script_directory, "output.txt") # had to use the os.path.join to get the correct path dynamically. Wasn't working with just the file name print(f"Input file: {input_file}\n, Output file: {output_file}\n") # troubleshooting the file name issues. Show the input and output in console. Censor_Words(input_file, output_file) # call the function to start censoring if __name__ == "__main__": __main__() # call the main function and get things started.