On Sun, 24 Apr 2022 at 21:11, Antoon Pardon <antoon.par...@vub.be> wrote: > > > > Op 23/04/2022 om 20:57 schreef Chris Angelico: > > On Sun, 24 Apr 2022 at 04:37, Marco Sulla<marco.sulla.pyt...@gmail.com> > > wrote: > >> What about introducing a method for text streams that reads the lines > >> from the bottom? Java has also a ReversedLinesFileReader with Apache > >> Commons IO. > > > > 1) Read the entire file and decode bytes to text > > 2) Split into lines > > 3) Iterate backwards over the lines > > > > Tada! Done. And in Python, quite easy. The downside, of course, is > > that you have to store the entire file in memory. > > Why not just do: > > tail = collections.deque(text_stream, maxlen = nr_of_lines) > tail.reverse() > ... >
You still need to read the entire file, and you also restrict the max line count, so you can't iterate this to take the next block of lines. ChrisA -- https://mail.python.org/mailman/listinfo/python-list