Get last line of a text file with Python
I subscribed myself to a newsletter that propose each week a new Python quiz, then answer it the day after. I accepted the challenge (for now, at least) and answer here when I’ve a solution to the quizzes. Let’s start with the first post with the first quiz.
The first question was:
“Ask the user for the name of a text file. Display the final line of that file.”
Seems quietly easy, here is my solution. I don’t like the fact I needed to “import sys” but didn’t find nothing better for asking a filename to user.
This instead was the “correct” answer:
Ok, first quiz and first interpretation error here. I wonder myself which part of “Ask the user” was not clear to me :-). My solution needs the filename passed as command line argument, and doesn’t ask the user for it. Next time I’ll try to understand better the posed question.
See ya!