• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Read Last N Lines of File in Python

Read Last N Lines of File in Python

July 11, 2022 Leave a Comment

To read the last N Lines of a file in Python, the easiest way is to use the Python file readlines() function and then access the last N elements of the returned list.

n = 5

with open("example.txt") as f:
    last_n_lines = f.readlines()[-n:]

When working with files, the ability to easily read from or write to a file is valuable.

One such case is if you want to read the last N lines of a file.

The easiest way to read the last N lines of a file in Python is with the Python file readlines() function.

readlines() reads all of the lines and returns a list.

After using readlines(), you can get the last N elements of the list, which will be the last N lines of the file.

Below is a simple example which shows you how to get the last N lines of a file with readlines() in Python.

n = 5

with open("example.txt") as f:
    last_n_lines = f.readlines()[-n:]

How to Read Last Line of File in Python

If you just want to read the last line of a file, you can use a simple method as above.

The easiest way, in our opinion, is with the file readlines() function. readlines() reads all of the lines and returns a list.

After using readlines(), you can get the last element of the list, which will be the last line of the file.

Below is an example showing how you can get the last line of a file using readlines() in Python.

with open("example.txt") as f:
    last_line = f.readlines()[-1]

Hopefully this article has been useful for you to learn how to read the last N lines of a file in Python.

Other Articles You'll Also Like:

  • 1.  Convert List to Set with Python
  • 2.  Get Days in Month Using Python
  • 3.  Read Pickle Files with pandas read_pickle Function
  • 4.  Print Approximately Equal Symbol in Python
  • 5.  Convert String into Tuple in Python
  • 6.  Print Time Elapsed in Python
  • 7.  Get First Key and Value in Dictionary with Python
  • 8.  Using Python to Print Degree Symbol
  • 9.  Python os.sep – Create Operating System Path Seperator Character
  • 10.  Create Empty File with Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy