Question: In Python, write a function that takes as its input a filename, and an integer. The file should open the file and read in the
In Python, write a function that takes as its input a filename, and an integer. The file should open the file and read in the first number of lines given as the second argument. (You'll need to have a variable to use as a counter for this part). Test your code using the file 'weblog.txt'. You can find this file on the 'Resources' page of the course web site.
You should then output those lines not to the screen, but to an output file called 'output_log.txt'. (That being said, I recommend doing this requirement last. To start with, output everything to the screen. This way, you won't have to keep opening the output file to see what is happening every tme you make a change to your code. Start by outputting to the screen, and when everything is working, modify your code to output to the file).
Example: Once you are finished, getLines('weblog.txt','40) would retrieve the first 40 lines of the file and output them to 'output_log.txt'.
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: 'Sorry, was unable to open that 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 ending.
Before outputting the requested numer of lines, first make sure that the number of lines the user asked for does not exceed the number of lines in the file. If the user does ask for too many lines, output: "Sorry, too many lines." and immediately end the function. How do you end a function prematurely? Simply with the statement 'return'. For example:
if something_sometthing:
return #ends the function
Ideally, we would handle this stuation using exception handling as well. We'll continue with exception handling in upcoming lectures.
HINT: As we have emphasized repeatedly, do not do too much at once. Work on a small piece of functionality, and once you are certain it is working properly, move on to the next bit of functionality. For example, start by reading in all the lines and outputting them to the screen. (Do the file output last so you dont have to keep opening the file to check your work!) Once that works, output only the number of lines the user asked for. Once that works, check to make sure they don't exceed the number of lines, etc, etc. Once all that works, then output to the output file instead of the screen.
checkLog weblog .txt 113) Sorry, too many lines. checkLog ('non existing file .txt', 3) Sorry, was unable to open that file. Traceback (most recent call last) File "
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
