开发者

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

开发者 https://www.devze.com 2022-12-18 06:15 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicate: Get last n lines of a file w开发者_StackOverflow社区ith Python, similar to tail
This question already has answers here: Closed 12 years ago.

Possible Duplicate:

Get last n lines of a file w开发者_StackOverflow社区ith Python, similar to tail

Hello,

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消