Python Program to Print Just the Last line of a Text File

Python Program to Print just the Last line of a Text File

In this article, we will be solving a very interesting question based on Data File Handling in Python. According to the question we have to read a text file and print just the last line of the text file.

If you like the article content, then share it in your coding groups so that more people can take advantage of the content. So, without wasting any time let's solve this question.

Question - Write a Python Program to Print just the Last line of a Text File



Logic and Explanation

Question - Write a Python Program to Print just the Last line of a Text File

Python Program to Print just the Last line of a Text File - In this program we have to first read the text file and for this we will have to open the text file in read file. Consider the text file "demo.txt". So, first open text File in Read Mode

with open("demo.txt","r"):


Now, we will be using the readlines method to read the text file. By using this method we get a list of strings in which each element is a line of the text file.

Then, for the last line we will print the last element of the list i.e. the element at index -1. That's all for this program. Now, let's write the code for the above.

with open("demo.txt","r") as f:
    line_list = f.readlines()
    print(line_list[-1])    


So, I hope you liked the article and you would have found the content of the article useful and helpful for you. Share this article in your coding communities so that more people can take advantage of the content. Also, In case of any doubt feel free to ask in the comments below.

For More Such Python Programs - Click Here

Enjoy Coding!!!


Also Read :-

Extract Phone Number Details Using Python





For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.

Post a Comment

For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.

Post a Comment (0)

Previous Post Next Post