How to read lines only at the end of file?

Christopher Koppler klapotec at
Fri Nov 7 09:55:25 CET 2003

On Fri, 07 Nov 2003 08:32:22 +0900, Kay Lee <aeronova at> wrote:

>I need to know how to make small scripts that acts like 'tail' command
>in unix with python.
>Any advice will be very helpful to me.
>Thanks in adv.

You could do the following:

>>> f = file('filename.txt', 'r')
>>> numberoflines = 10
>>> for line in f.readlines()[-numberoflines:]:
...     print line

which prints the last 10 lines of the file.


More information about the Python-list mailing list