Question: The objective of this programming assignment is to modify an existing text file, replacing words. All you have to turn in is the Python code.This
The objective of this programming assignment is to modify an existing text file, replacing words. All you have to turn in is the Python code.This assignment will incorporate the conditional and repetition patterns you have done previously, and also incorporate file inputoutput and exception handling.Starting with a plain text file, which you can create using Notepad or other common text editing tool, create a file that contains some simple text. An easy way to do this is to use a common website, such as this one. Many web developers use such sites to create placeholder text as a standin until a copy editor replaces it with the actual text later. Feel free to create your own text file. Note: do NOT use editors such as Microsoft Word, WordPad, and the like, because files like doc or docx are not plain old text files and won't work for this assignment. Write a Python program that:Ask the user for a filenameVerify that the filename exists or print an error and stop if it doesn't existOpen the file remember to catch any exceptions that might be thrown, and print an error if that happensAsk the user for two words, the search word and the replacement wordRead each line of the file, and for each line, see if it contains the search word. If it does, replace it with the replacement word. Hmmm, how do we do that? One pattern is to create a new string concatenating what comes before the search word, the replacement word, and what comes after the search word.Hmmm, how do we "replace" the line in the file? One pattern is to create a new file, write the lines to the new file as we read them, and if the search word appears, write the modified line to the new file instead of the original line. Then when we are finished, close both files and rename the new file with the original file's filename. Tricky? Hey, this is how every program on your computer must handle files!Since we are comfortable doing assignments, let's polish it up this time and make it look professional. Include comments at the top of your Python code file with your name, the class, and a brief assignment description. Display a nice prompt message at the beginning of the program with the title and version.Comment your Python code appropriately.Before exiting, display a nice message that thanks the user for using your program and report how many lines were modified.Testing be sure to test your code:What happens if the filename entered doesn't exist?What if the file exists, but is opened in an editor?What if the search word isn't found in the file?What if the search word appears twice on the same line?
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
