Skip last 5 lines in file using python

I wanted to delete the last few lines in a file using python. The file is huge, so I use the following code to delete the first line

import sys
with open(sys.argv[1],"rb") as f:
    for _ in range(6):#skip first 6 lines
        next(f)
    for line in f:
        print line
+4
source share
3 answers

Here's a generic generator to truncate any iterable:

from collections import deque

def truncate(iterable, num):
    buffer = deque(maxlen=num)
    iterator = iter(iterable)

    # Initialize buffer
    for n in range(num):
        buffer.append(next(iterator))

    for item in iterator:
        yield buffer.popleft()
        buffer.append(item)

truncated_range20 = truncate(range(20), 5)

print(list(truncated_range20))
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

Using truncate, you can do this:

from __future__ import print_function

import sys

from itertools import islice


filepath = sys.argv[1]

with open(filepath, 'rb') as f:
    for line in truncate(islice(f, 6, None), 5):
        print(line, end='')
+3
source

If each line has a different length, and you cannot predict when to stop with the file size, your python script has no way to find out.

, . , , 5, , , , , .

5 ?

import sys

with open(sys.argv[1],"rb") as f:
    # Skip 6 lines
    for _ in range(6):
        next(f)

    # Create a list that will contain at most 5 lines.
    # Using a list is not super efficient here (a Queue would be better), but it only 5 items so...
    last_lines = []
    for line in f:
        # if the buffer is full, print the first one and remove it from the list.
        if len(last_lines) == 5:
            print last_lines.pop(0)

        # append current line to the list.
        last_lines.append(line)

    # when we reach this comment, the last 5 lines will remain on the list.
    # so you can just drop them.

, , , python, " " - .

, "head" "tail" ( , Windows), ( , , , python ..).

+3

.

, . , . , 5 , :

import os 

back_up = 5 * 200       # Go back from the end more than 5 lines worth

with open("foo.txt", "r+") as f:
    f.seek(-back_up, os.SEEK_END)
    lines = f.readlines()[:-5]
    f.seek(-back_up, os.SEEK_END)
    f.write("".join(lines))
    f.truncate()

, , . , , .

, , back_up , . 10 * 10000, . .

0

All Articles