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.
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.
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")
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