Search code examples
pythonfilefile-iotail

Read last n lines of file (tail) without reading it line-by-line?


Possible Duplicate:
Get last n lines of a file with Python, similar to tail

Hello,

How can I have Python return the last n lines of a file without reading it line by line?


Solution

  • Something like this:

    • Use seek() to get something like the last 4096 bytes of a file.
    • See how many newlines you have in those bytes. If you have n or more, then you're done. If you have fewer, then read the previous 4096 bytes until you're done.

    Not sure if there's a built-in way to do this.