Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1045701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:01:37+00:00 2026-05-16T16:01:37+00:00

I need to know how to read lines from a file in python so

  • 0

I need to know how to read lines from a file in python so that I read the last line first and continue in that fashion until the cursor reach’s the beginning of the file. Any idea’s?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T16:01:38+00:00Added an answer on May 16, 2026 at 4:01 pm

    The general approach to this problem, reading a text file in reverse, line-wise, can be solved by at least three methods.

    The general problem is that since each line can have a different length, you can’t know beforehand where each line starts in the file, nor how many of them there are. This means you need to apply some logic to the problem.

    General approach #1: Read the entire file into memory

    With this approach, you simply read the entire file into memory, in some data structure that subsequently allows you to process the list of lines in reverse. A stack, a doubly linked list, or even an array can do this.

    Pros: Really easy to implement (probably built into Python for all I know)
    Cons: Uses a lot of memory, can take a while to read large files

    General approach #2: Read the entire file, store position of lines

    With this approach, you also read through the entire file once, but instead of storing the entire file (all the text) in memory, you only store the binary positions inside the file where each line started. You can store these positions in a similar data structure as the one storing the lines in the first approach.

    Whever you want to read line X, you have to re-read the line from the file, starting at the position you stored for the start of that line.

    Pros: Almost as easy to implement as the first approach
    Cons: can take a while to read large files

    General approach #3: Read the file in reverse, and “figure it out”

    With this approach you will read the file block-wise or similar, from the end, and see where the ends are. You basically have a buffer, of say, 4096 bytes, and process the last line of that buffer. When your processing, which has to move one line at a time backward in that buffer, comes to the start of the buffer, you need to read another buffer worth of data, from the area before the first buffer you read, and continue processing.

    This approach is generally more complicated, because you need to handle such things as lines being broken over two buffers, and long lines could even cover more than two buffers.

    It is, however, the one that would require the least amount of memory, and for really large files, it might also be worth doing this to avoid reading through gigabytes of information first.

    Pros: Uses little memory, does not require you to read the entire file first
    Cons: Much hard to implement and get right for all corner cases


    There are numerous links on the net that shows how to do the third approach:

    • ActiveState Recipe 120686 – Read a text file backwards
    • ActiveState Recipe 439045 – Read a text file backwards (yet another implementation)
    • Top4Download.com Script – Read a text file backwards
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to have python read some lines of text from a file and
I need to read lines from a text file but, where the 'end of
I need to read line by line from text file (log files from server)
I have a data file, which I need to read. I know to read
I need to read a file in PHP, but I only know the end
I know about a = gets, but I need to read characters from keyboard
I would like to open a file and read a line from it. There
i need to get the string from on php file someone know how can
I need to read one block from file and then need to match the
I'm reading in from a .txt file that looks something along the lines of:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.