Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read and delete first n lines from file in Python - Elegant Solution [duplicate]

I have a pretty big file ~ 1MB in size and I would like to be able to read first N lines, save them into a list (newlist) for later use, and then delete them.

I am able to do that like this:

import os

n = 3 #the number of line to be read and deleted

with open("bigFile.txt") as f:
    mylist = f.read().splitlines()

newlist = mylist[:n]
os.remove("bigFile.txt")

thefile = open('bigFile.txt', 'w')

del mylist[:n]

for item in mylist:
  thefile.write("%s\n" % item)

I know this doesn't look good in terms of efficiency and this is why I need something better but after searching for different solutions I was stuck with this one.

like image 939
Stephanie Safflower Avatar asked Mar 06 '17 16:03

Stephanie Safflower


People also ask

How do you remove a line from a file in Python?

Method 1: Deleting a line using a specific position In this method, the text file is read line by line using readlines(). If a line has a position similar to the position to be deleted, it is not written in the newly created text file.


1 Answers

A file is it's own iterator.

n = 3
nfirstlines = []

with open("bigFile.txt") as f, open("bigfiletmp.txt", "w") as out:
    for x in xrange(n):
        nfirstlines.append(next(f))
    for line in f:
        out.write(line)

# NB : it seems that `os.rename()` complains on some systems
# if the destination file already exists.
os.remove("bigfile.txt")
os.rename("bigfiletmp.txt", "bigfile.txt")
like image 65
bruno desthuilliers Avatar answered Oct 18 '22 18:10

bruno desthuilliers