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?
Use file. readline() to read a single line from a file readline() to get the first line of the file and store this in a variable first_line . Create a second variable, last_line , and iterate through all lines in the file until the end. Once the iteration ends, last_line will contain the last line of the file.
Method 1: fileobject.readlines() A file object can be created in Python and then readlines() method can be invoked on this object to read lines into a stream. This method is preferred when a single line or a range of lines from a file needs to be accessed simultaneously.
Recipe 120686: Read a text file backwards (Python)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With