Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove last N lines from txt file with Python?

Tags:

python

I would like to know what is the fastest and more efficient way of deleting last N lines of a .txt file.

I have been looking for different previous questions and I found that reading and copying lines again is one way.

Remove lines from textfile with python

Nonetheless, I have been trying to look for other way such as skiprows or skipfooter on pandas and be able to save the outcome to other txt.

At the same time, I have seen threads about "head" and "sed" but not sure about their usage in Python.

Could you, please, give advice on this topic?

like image 921
FJSJ Avatar asked Jan 28 '19 10:01

FJSJ


People also ask

How do you remove lines from a text file in Python?

with open(inputFile, 'w') as filedata: Traverse in each line of the file using the for loop. Enter the line number to be deleted as dynamic input using the input() function (The input() function reads a line from the input (from the user), converts it to a string by eliminating the trailing newline, and returns it.

How do I read the last n lines of a file in Python?

As we know, Python provides multiple in-built features and modules for handling files. Let's discuss different ways to read last N lines of a file using Python. In this approach, the idea is to use a negative iterator with the readlines() function to read all the lines requested by the user from the end of file.


1 Answers

if you want to skip last n lines using pandas then:

import pandas as pd

df = pd.read_csv('yourfile.txt', skipfooter = N)
df.to_csv('yournewfile.txt')

Change the delimiter using sep = ... if necessary

Alternatively do as per the answer you cited, but using with open(..) as good practice:

with open('yourfile.txt') as f1:
    lines = f1.readlines()

with open('yournewfile.txt', 'w') as f2:
    f2.writelines(lines[:-N])
like image 128
jambannabelbar Avatar answered Sep 24 '22 02:09

jambannabelbar