runtime
181 / 500
question 181 Files & Directories · easy

The last lines of a log

When something breaks, the first thing anyone looks at is the end of the log. Write a function that returns the last few lines of a log file.

Functiontail_lines(path, n)
Returnsa list of the last n lines of the file, without their newlines, in file order
Rulesfewer lines than n gives all of them; n of 0 gives []; a blank line is still a line ("")
server.logfour lines: 08:59 store opened, then three order … packed lines
Examples
calltail_lines('server.log', 2)returns['09:40 order O101217 packed', '10:02 order O104839 packed']
calltail_lines('server.log', 10)returns['08:59 store opened', '09:14 order O100043 packed', '09:40 order O101217 packed', '10:02 order O104839 packed']
calltail_lines('empty.log', 3)returns[]

Loading your progress…

The last lines of a log — Python practice · Runtime