📜  Python – 读取文件的最后 N 行

📅  最后修改于: 2022-05-13 01:55:26.248000             🧑  作者: Mango

Python – 读取文件的最后 N 行

先决条件:在Python中逐行读取文件
给定一个文本文件fname ,一个数字N ,任务是读取文件的最后 N 行。
众所周知, Python提供了多种用于处理文件的内置功能和模块。让我们讨论使用Python读取文件最后 N 行的不同方法。
文件:

文件内容的图像

方法一:天真的方法
在这种方法中,想法是使用带有 readlines()函数的负迭代器从文件末尾读取用户请求的所有行。

Python3
# Python implementation to
# read last N lines of a file
 
# Function to read
# last N lines of the file
def LastNlines(fname, N):
    # opening file using with() method
    # so that file get closed
    # after completing work
    with open(fname) as file:
         
        # loop to read iterate
        # last n lines and print it
        for line in (file.readlines() [-N:]):
            print(line, end ='')
 
 
# Driver Code:
if __name__ == '__main__':
    fname = 'File1.txt'
    N = 3
    try:
        LastNlines(fname, N)
    except:
        print('File not found'


Python3
# Python implementation to
# read last N lines of a file
# Using OS module and buffering policy
         
# importing os module
import os
 
# Function to read
# last N lines of the file
def LastNlines(fname, N):
    # taking buffer size of 8192 bytes
    bufsize = 8192
     
    # calculating size of
    # file in bytes
    fsize = os.stat(fname).st_size
     
    iter = 0
     
    # opening file using with() method
    # so that file get closed
    # after completing work
    with open(fname) as f:
        if bufsize > fsize:
                 
            # adjusting buffer size
            # according to size
            # of file
            bufsize = fsize-1
             
            # list to store
            # last N lines
            fetched_lines = []
             
            # while loop to
            # fetch last N lines
            while True:
                iter += 1
                 
                # moving cursor to
                # the last Nth line
                # of file
                f.seek(fsize-bufsize * iter)
                 
                # storing each line
                # in list upto
                # end of file
                fetched_lines.extend(f.readlines())
                 
                # halting the program
                # when size of list
                # is equal or greater to
                # the number of lines requested or
                # when we reach end of file
                if len(fetched_lines) >= N or f.tell() == 0:
                        print(''.join(fetched_lines[-N:]))
                        break
                         
# Driver Code:
if __name__ == '__main__':
     
    fname = 'File1.txt'
    N = 3
     
    try:
        LastNlines(fname, N)
    except:
        print('File not found')


Python3
# Python implementation to
# read last N lines of a file
# through Exponential search
         
# Function to read
# last N lines of the file
def LastNlines(fname, N):
     
    # assert statement check
    # a condition
    assert N >= 0
     
    # declaring variable
    # to implement
    # exponential search
    pos = N + 1
     
    # list to store
    # last N lines
    lines = []
     
    # opening file using with() method
    # so that file get closed
    # after completing work
    with open(fname) as f:
         
        # loop which runs
        # until size of list
        # becomes equal to N
        while len(lines) <= N:
             
            # try block
            try:
                # moving cursor from
                # left side to
                # pos line from end
                f.seek(-pos, 2)
         
            # exception block
            # to handle any run
            # time error
            except IOError:
                f.seek(0)
                break
             
            # finally block
            # to add lines
            # to list after
            # each iteration
            finally:
                lines = list(f)
             
            # increasing value
            # of variable
            # exponentially
            pos *= 2
             
    # returning the
    # whole list
    # which stores last
    # N lines
    return lines[-N:]
 
# Driver Code:
if __name__ == '__main__':
    fname = 'File1.txt'
    N = 3
    try:
        lines = LastNlines(fname, N)
        for line in lines:
            print (line, end ='')
    except:
        print('File not found')


输出:

Eighth line
Ninth line
Tenth line


方法二:使用操作系统模块和缓冲策略
在这种方法中,想法是在Python中处理缓冲策略。缓冲区存储从操作系统的文件流接收到的部分数据,并在使用一段时间后进入更多数据。
缓冲区大小决定了在使用之前一次可以存储的数据的大小。我们可以选择将整数传递给缓冲以设置缓冲策略,如果我们不指定任何策略,则缓冲区的大小取决于设备的块大小。通常,缓冲区的长度为 4096 或 8192 字节。在这种方法中,缓冲区的大小为 8192 字节。
此外,OS 模块中 os.stat() 方法的st_size属性用于表示文件的大小(以字节为单位)。
下面是上述方法的实现。

Python3

# Python implementation to
# read last N lines of a file
# Using OS module and buffering policy
         
# importing os module
import os
 
# Function to read
# last N lines of the file
def LastNlines(fname, N):
    # taking buffer size of 8192 bytes
    bufsize = 8192
     
    # calculating size of
    # file in bytes
    fsize = os.stat(fname).st_size
     
    iter = 0
     
    # opening file using with() method
    # so that file get closed
    # after completing work
    with open(fname) as f:
        if bufsize > fsize:
                 
            # adjusting buffer size
            # according to size
            # of file
            bufsize = fsize-1
             
            # list to store
            # last N lines
            fetched_lines = []
             
            # while loop to
            # fetch last N lines
            while True:
                iter += 1
                 
                # moving cursor to
                # the last Nth line
                # of file
                f.seek(fsize-bufsize * iter)
                 
                # storing each line
                # in list upto
                # end of file
                fetched_lines.extend(f.readlines())
                 
                # halting the program
                # when size of list
                # is equal or greater to
                # the number of lines requested or
                # when we reach end of file
                if len(fetched_lines) >= N or f.tell() == 0:
                        print(''.join(fetched_lines[-N:]))
                        break
                         
# Driver Code:
if __name__ == '__main__':
     
    fname = 'File1.txt'
    N = 3
     
    try:
        LastNlines(fname, N)
    except:
        print('File not found')

输出:

Eighth line
Ninth line
Tenth line


方法3:通过指数搜索
在这种方法中,想法是使用指数搜索算法,该算法通常用于搜索排序的、无界的或无限的列表。要获取有关指数搜索的信息,请单击此处。
这种方法使用断言语句作为调试工具来检查条件。如果给定的语句为真,程序将继续执行,否则会产生AssertionError 异常。要获取有关断言语句的更多详细信息,请单击此处。
单击此处以熟悉 seek() 方法的不同用途。
下面是上述方法的实现。

Python3

# Python implementation to
# read last N lines of a file
# through Exponential search
         
# Function to read
# last N lines of the file
def LastNlines(fname, N):
     
    # assert statement check
    # a condition
    assert N >= 0
     
    # declaring variable
    # to implement
    # exponential search
    pos = N + 1
     
    # list to store
    # last N lines
    lines = []
     
    # opening file using with() method
    # so that file get closed
    # after completing work
    with open(fname) as f:
         
        # loop which runs
        # until size of list
        # becomes equal to N
        while len(lines) <= N:
             
            # try block
            try:
                # moving cursor from
                # left side to
                # pos line from end
                f.seek(-pos, 2)
         
            # exception block
            # to handle any run
            # time error
            except IOError:
                f.seek(0)
                break
             
            # finally block
            # to add lines
            # to list after
            # each iteration
            finally:
                lines = list(f)
             
            # increasing value
            # of variable
            # exponentially
            pos *= 2
             
    # returning the
    # whole list
    # which stores last
    # N lines
    return lines[-N:]
 
# Driver Code:
if __name__ == '__main__':
    fname = 'File1.txt'
    N = 3
    try:
        lines = LastNlines(fname, N)
        for line in lines:
            print (line, end ='')
    except:
        print('File not found')

输出:

Eighth line
Ninth line
Tenth line