利用队列的内置模块(deque)模拟 Linux 下的 tail 命令(输出文件中最后几行的内容)

zylyehuo / 2023-08-17 / 原文

# -*- coding: utf-8 -*-

from collections import deque

def tail(n):  # n:指定输出文件中最后几行
    with open('test.txt', 'r') as f:
        q = deque(f, n)
        return q

for line in tail(5):
    print(line, end='')