How to print the last 10 lines from a text file?

Hi guys, been googling for about 20 minutes without no luck. All I find is how to print out the last line or how to find the last line… but not how to print the 10 last lines.

This is the code:

f = open('textfil.txt', 'r')
for line in f:
    print(line)
f.close()

This prints out the lines in the text file but how do I do to just print out the 10 last lines? Using loop if possible,
Example if the lines are:
1
2
3
4
5
6
7
8
9
10
11
Then the print should be:
2
3
4
5
6
7
8
9
10
11
Would also like to know how to print the 10 last lines in reverse if you guys know how to like this:
11
10
9
8
and so on…

Thanks in advance for the help!

we could use a list, python has a built-in function to achieve this:

Learning about the readlines() function in Python

once you have the list, you start slicing. You might be able to do this in one step

How do I slice the 10 last elements and how do I do it in reverse also?

what is reverse? The first 10 elements?

isn’t this something you can google? (or whatever search engine you use). I know the answer, but finding the information you are looking for as a valuable skill as developer. The earlier you start, the better :slight_smile:

1 Like

As I said in the beginning of my post I spent 20 minutes googling without luck and that is why I’m posting here. I have found how to print the last 10 elements but not how I want it it’s printing it out in list form and not like I posted how I wanted it.

I googled:

python list slice last n elements

first thing I got:

How to get last items of a list in Python? - Stack Overflow

once you have completed the slicing, you will still need a loop to print the result pretty

f = open('textfil.txt', 'r')
lista = f.readlines()
lista = lista[-10:]
for i in lista:
    print(i)
f.close()

This worked but is it any better way? And how do I print in reverse?

better how?

lists can simple be reversed? There are built-in function/methods for that

Nevermind I thought there might be some function I could use to just print the 10 last word directly without these steps.

f = open('textfil.txt', 'r')
lista = f.readlines()
lista = lista[-10:]
lista.reverse()
for i in lista:
    print(i)
f.close()

Got it to work with this but why do it print the first 2 lines close to each other? How do I fix that?

If this is the text file:

1
2
3
4
5
6
7
8
9
10
11

I get

11
10

9

8

7

6

5

4

3

2

Why is the first two numbers close to each other and not the rest, how do I fix this? How do I seperate 11 and 10 or put the rest of the numbers closer to each other?

you could write such a a function? I think you can also merge these two steps together into:

lista = f.readlines()[-10:]

you could print the list:

print(lista)

to see if the values in the list are correct. That might shed some light on where things go wrong (reading from the file, or the printing). The file might contain new line characters for example.

1 Like

Thanks for the merging tip and taking your time helping me. I printed the list and saw the problem, thanks for helping me understanding!

1 Like