avatar Bite 100. Display the last part of a file (Unix tail)

Complete the function below simulating Unix' tail, for example:

$ tail -3 test_tail.py
# byte to str conversion and strip off last line's newline char
expected = [line.decode("utf-8") for line in lines]
assert tail(f.name, 10) == expected

Complete the function below which receives (absolute) filepath and n lines to filter from the end which is returned in a list.

For example, if we call it on a file - stored in filepath - with this content:

Hello world!
We hope that you are learning a lot of Python.
Have fun with our Bites of Py.
Keep calm and code in Python!
Become a PyBites ninja!

... and give it n of 2 (= calling it as: tail(filepath, 2)), it should return this:

['Keep calm and code in Python!',
 'Become a PyBites ninja!']

(note: newlines are stripped off)

Have fun and let us know if you have any questions ...

Login and get coding
go back Beginner level
Bitecoin 2X

425 out of 429 users completed this Bite.
Will you be the 426th person to crack this Bite?
Resolution time: ~29 min. (avg. submissions of 5-240 min.)
Our community rates this Bite 2.8 on a 1-10 difficulty scale.
» You can do it! 😌

Focus on this Bite hiding sidebars, turn on Focus Mode.

Ask for Help