Question: In Python, write a function called printLines() that takes one input argument, a file name, and outputs every line in the file, but preceded by
In Python, write a function called printLines() that takes one input argument, a file name, and outputs every line in the file, but preceded by the word "Line", the line number, and a tab. To include a tab in your code, use '\t'. The \t character works just like the character we have been discussing. That is, it includes a tab in your print output.
Hint: Note that you will also probably want to sue both the sep and end arguments in your print() function. First try things without these arguments, and then when you see the results, add them in (one at a time and always check your work in between!).
All functions must include a brief docstring comment indicating what the function does.
Your program should use exception handling to look for either an IOError or a FileNotFoundError when the opening the input file. If the program can't open the file, print a line that says: 'Unable to open file'.
Note: For now, your program will still end when the exception is generated. However, by printing some useful output information, you have at least given the user an idea of what went wrong. In an upcoming lecture, we will discuss some ways of allowing the program to continue without crashing.
If used on our 'example.txt' file it would look like the following:
print Lines example. txt') Line #1 Here is a random file Line #2 This is the second line. Line #3 Line #4 There is a blank line above this line. printLines non existing file.txt unable to open file Traceback (most recent call last) File "xpy shell 22 line 1, inStep by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
