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?
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.
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.
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])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With