当前位置:  开发笔记 > 编程语言 > 正文

使用Python获取文件的最后n行,类似于tail

如何解决《使用Python获取文件的最后n行,类似于tail》经验,为你挑选了7个好方法。

我正在为Web应用程序编写一个日志文件查看器,为此我想通过日志文件的行分页.文件中的项目是基于行的,底部是最新项目.

所以我需要一种tail()方法,可以n从底部读取行并支持偏移量.我想出的是这样的:

def tail(f, n, offset=0):
    """Reads a n lines from f with an offset of offset lines."""
    avg_line_length = 74
    to_read = n + offset
    while 1:
        try:
            f.seek(-(avg_line_length * to_read), 2)
        except IOError:
            # woops.  apparently file is smaller than what we want
            # to step back, go to the beginning instead
            f.seek(0)
        pos = f.tell()
        lines = f.read().splitlines()
        if len(lines) >= to_read or pos == 0:
            return lines[-to_read:offset and -offset or None]
        avg_line_length *= 1.3

这是一种合理的方法吗?使用偏移量尾部日志文件的推荐方法是什么?



1> S.Lott..:

这可能比你的更快.不对线长做出假设.一次一个块地返回文件,直到找到正确数量的'\n'字符.

def tail( f, lines=20 ):
    total_lines_wanted = lines

    BLOCK_SIZE = 1024
    f.seek(0, 2)
    block_end_byte = f.tell()
    lines_to_go = total_lines_wanted
    block_number = -1
    blocks = [] # blocks of size BLOCK_SIZE, in reverse order starting
                # from the end of the file
    while lines_to_go > 0 and block_end_byte > 0:
        if (block_end_byte - BLOCK_SIZE > 0):
            # read the last block we haven't yet read
            f.seek(block_number*BLOCK_SIZE, 2)
            blocks.append(f.read(BLOCK_SIZE))
        else:
            # file too small, start from begining
            f.seek(0,0)
            # only read what was not read
            blocks.append(f.read(block_end_byte))
        lines_found = blocks[-1].count('\n')
        lines_to_go -= lines_found
        block_end_byte -= BLOCK_SIZE
        block_number -= 1
    all_read_text = ''.join(reversed(blocks))
    return '\n'.join(all_read_text.splitlines()[-total_lines_wanted:])

我不喜欢关于线长的棘手假设 - 作为一个实际问题 - 你永远不会知道这样的事情.

通常,这将在第一次或第二次通过循环时找到最后20行.如果你的74个角色实际上是准确的,你可以使块大小为2048,你几乎可以立即尾随20行.

此外,我没有燃烧大量的脑热量,试图巧妙地与物理OS块对齐.使用这些高级I/O包,我怀疑你会看到尝试在OS块边界上对齐的任何性能后果.如果您使用较低级别的I/O,那么您可能会看到加速.


这在小日志文件上失败 - IOError:无效参数 - f.seek(块*1024,2)
请勿使用此代码.它破坏了python 2.7中某些边界情况下的行.以下@papercrane的答案解决了这个问题.
不再适用于python 3.2.我得到`io.UnsupportedOperation:不能做非零的终端相对搜索`我可以将偏移更改为0,但这会破坏函数的用途.
@DavidEnglund原因是[这里](http://www.velocityreviews.com/forums/t748976-python-3-2-bug-reading-the-last-line-of-a-file.html).简而言之:在文本模式下不允许相对于文件末尾的搜索,大概是因为文件内容必须被解码,并且一般来说,在编码字节序列中寻找任意位置时可能会有不确定的结果尝试从该位置开始解码为Unicode.该链接提供的建议是尝试以二进制模式打开文件并自己进行解码,捕获DecodeError异常.

2> Mark..:

假设你可以在Python 2上使用类似unix的系统:

import os
def tail(f, n, offset=0):
  stdin,stdout = os.popen2("tail -n "+n+offset+" "+f)
  stdin.close()
  lines = stdout.readlines(); stdout.close()
  return lines[:,-offset]

对于python 3,您可以这样做:

import subprocess
def tail(f, n, offset=0):
    proc = subprocess.Popen(['tail', '-n', n + offset, f], stdout=subprocess.PIPE)
    lines = proc.stdout.readlines()
    return lines[:, -offset]


问题并不是说平台依赖是不可接受的.我不明白为什么这应该得到两个downvotes,当它提供一个非常unixy(可能是你正在寻找的......当然是对我来说)的方式正是在做问题所要求的.
您可能希望预先计算偏移量,如:`offset_total = str(n + offset)`并将此行替换为`stdin,stdout = os.popen2("tail -n"+ offset_total +""+ f)`以避免`TypeErrors(无法连接int + str)`
应该是平台独立的.此外,如果您阅读该问题,您将看到f是一个类似对象的文件.
谢谢,我以为我必须用纯Python来解决这个问题,但没有理由不使用UNIX实用程序,所以我选择了这个.现代Python中的FWIW,subprocess.check_output可能比os.popen2更可取; 它简化了一些事情,因为它只是将输出作为字符串返回,并引发非零退出代码.
虽然这是依赖于平台的,但它是一种非常有效的方式来做所谓的事情,并且是一种非常快速的方法(你不必将整个文件加载到内存中).@Shabbyrobe
考虑`tail('file.txt; rm -rf /',10)`(**不要打电话给**)
@Mark更新可能会很好,因为popen2自pyton2.6以来已被弃用

3> A. Coady..:

如果读取整个文件是可以接受的,那么使用双端队列.

from collections import deque
deque(f, maxlen=n)

在2.6之前,deques没有maxlen选项,但它很容易实现.

import itertools
def maxque(items, size):
    items = iter(items)
    q = deque(itertools.islice(items, size))
    for item in items:
        del q[0]
        q.append(item)
    return q

如果要求从最后读取文件,则使用驰骋(又称指数)搜索.

def tail(f, n):
    assert n >= 0
    pos, lines = n+1, []
    while len(lines) <= n:
        try:
            f.seek(-pos, 2)
        except IOError:
            f.seek(0)
            break
        finally:
            lines = list(f)
        pos *= 2
    return lines[-n:]



4> glenbot..:

这是我的答案.纯蟒蛇.使用timeit似乎很快.拖尾100行有100,000行的日志文件:

>>> timeit.timeit('tail.tail(f, 100, 4098)', 'import tail; f = open("log.txt", "r");', number=10)
0.0014600753784179688
>>> timeit.timeit('tail.tail(f, 100, 4098)', 'import tail; f = open("log.txt", "r");', number=100)
0.00899195671081543
>>> timeit.timeit('tail.tail(f, 100, 4098)', 'import tail; f = open("log.txt", "r");', number=1000)
0.05842900276184082
>>> timeit.timeit('tail.tail(f, 100, 4098)', 'import tail; f = open("log.txt", "r");', number=10000)
0.5394978523254395
>>> timeit.timeit('tail.tail(f, 100, 4098)', 'import tail; f = open("log.txt", "r");', number=100000)
5.377126932144165

这是代码:

import os


def tail(f, lines=1, _buffer=4098):
    """Tail a file and get X lines from the end"""
    # place holder for the lines found
    lines_found = []

    # block counter will be multiplied by buffer
    # to get the block size from the end
    block_counter = -1

    # loop until we find X lines
    while len(lines_found) < lines:
        try:
            f.seek(block_counter * _buffer, os.SEEK_END)
        except IOError:  # either file is too small, or too many lines requested
            f.seek(0)
            lines_found = f.readlines()
            break

        lines_found = f.readlines()

        # we found enough lines, get out
        # Removed this line because it was redundant the while will catch
        # it, I left it for history
        # if len(lines_found) > lines:
        #    break

        # decrement the block counter to get the
        # next X bytes
        block_counter -= 1

    return lines_found[-lines:]


优雅的解决方 `if len(lines_found)>行:`真的有必要吗?`loop`条件不会也能抓住它吗?
@MaximilianPeters是的。这不是必需的。我把它注释掉了。

5> papercrane..:

S.Lott上面的答案几乎对我有用,但最终给了我部分线条.事实证明它破坏了块边界上的数据,因为数据以相反的顺序保持读取块.当调用'.join(数据)时,块的顺序错误.这解决了这个问题.

def tail(f, window=20):
    """
    Returns the last `window` lines of file `f` as a list.
    f - a byte file-like object
    """
    if window == 0:
        return []
    BUFSIZ = 1024
    f.seek(0, 2)
    bytes = f.tell()
    size = window + 1
    block = -1
    data = []
    while size > 0 and bytes > 0:
        if bytes - BUFSIZ > 0:
            # Seek back one whole BUFSIZ
            f.seek(block * BUFSIZ, 2)
            # read BUFFER
            data.insert(0, f.read(BUFSIZ))
        else:
            # file too small, start from begining
            f.seek(0,0)
            # only read what was not read
            data.insert(0, f.read(bytes))
        linesFound = data[0].count('\n')
        size -= linesFound
        bytes -= BUFSIZ
        block -= 1
    return ''.join(data).splitlines()[-window:]



6> Armin Ronach..:

我最终使用的代码.我认为这是迄今为止最好的:

def tail(f, n, offset=None):
    """Reads a n lines from f with an offset of offset lines.  The return
    value is a tuple in the form ``(lines, has_more)`` where `has_more` is
    an indicator that is `True` if there are more lines in the file.
    """
    avg_line_length = 74
    to_read = n + (offset or 0)

    while 1:
        try:
            f.seek(-(avg_line_length * to_read), 2)
        except IOError:
            # woops.  apparently file is smaller than what we want
            # to step back, go to the beginning instead
            f.seek(0)
        pos = f.tell()
        lines = f.read().splitlines()
        if len(lines) >= to_read or pos == 0:
            return lines[-to_read:offset and -offset or None], \
                   len(lines) > to_read or pos > 0
        avg_line_length *= 1.3


没有完全回答这个问题.

7> dimitri..:

使用mmap简单快速的解决方案:

import mmap
import os

def tail(filename, n):
    """Returns last n lines from the filename. No exception handling"""
    size = os.path.getsize(filename)
    with open(filename, "rb") as f:
        # for Windows the mmap parameters are different
        fm = mmap.mmap(f.fileno(), 0, mmap.MAP_SHARED, mmap.PROT_READ)
        try:
            for i in xrange(size - 1, -1, -1):
                if fm[i] == '\n':
                    n -= 1
                    if n == -1:
                        break
            return fm[i + 1 if i else 0:].splitlines()
        finally:
            fm.close()

推荐阅读
和谐啄木鸟
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有