Question: Python Complete the process_file() function that takes a single string parameter, filename, the name of the file to be processed. The data in the file
Python
Complete the process_file() function that takes a single string parameter, filename, the name of the file to be processed. The data in the file is organized in pairs of lines. The first line of the pair specifies an index into the string specified by the second line of the pair. You can assume that the file will never be empty and will always have an even number of lines. For each pair of lines, the function should print the character in the string on the second line at the index specified by the first line. For example if a pair of lines are: 5 awesome The function would print Character: m The process_file() function must handle the following situations: If the file specified by filename cannot be found the function should print the message: Error: The file XXX does not exist where XXX indicates the filename. If the first line in a pair of lines is not a valid index, the function should print Error: YYY is not a valid index where YYY is the data on this line. If the index specified by the first line is out of range with respect to the string on the second line, the function should print Error: index XXX is out of range for the string YYY where XXX is the index and YYY is the string. Things to note: You must use one or more try and except blocks to handle any exceptions that might occur. You must close the file properly if the file can be opened. Some examples of the function being used are shown below. You can download some sample input files here. For example: Test Result process_file("Imaginary.txt") Error: The file Imaginary.txt does not exist process_file("File1.txt") Character: 0 Error: a is not a valid index Error: index 65 is out of range for the string: Too short! Character: W process_file("File2.txt") Error: index 3 is out of range for the string: Error: Yes is not a valid index Character:n Error: is not a valid index
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
