All other answers are valid so you probably got a good idea on how to do it by loading the file, changing the content and then saving the file back.
I just want to point out that there is a possibility of changing the content of the file directly in storage memory. It is not always wise to do so, the thing has its drawbacks, but as it may be useful for some future usages there it is.
To change (delete or insert) content into an existing file you can use the mmap module.
It allows you to map a portion of the RAM or storage memory (file) and access and edit it like it is a string. Perhaps a list it's better to say.
So, to remove the line you want you open the file, load its content and perform find() or something else to find the index of the line you want to delete and its length.
Then you memory map the file, and simply move the rest of the content following the line you wish to delete upward, thus "covering" the unwanted line. You do it using slicing. Then you resize the memory map to cut off the remaining bytes after you shifted the content. Thus you resize the file to right size and then you can close the mmap which will not close the file.
You can insert a line in this manner into a file. You first resize the file, shift the content toward the end to make a space where you want your line to appear, and joust write it in.
It sounds a bit complicated and a lot of work, but it isn't really. It saves you the trouble of writing the whole file each time you remove the line.
I didn't check how fast it is, and is it faster than overwrite each time. But this is one solution I felt worth mentioning.
Here is some quickly assembled code:
# This needs checks and rechecks
# Also, its efficiency is questionable. Some optimization can be done with find() and rfind()
# But we can choose to believe in the module and just do it.
# The nice thing is that we can use find() to point the user, not searching for the line number like mad.
from mmap import mmap
def removeline (fname, nl):
f = open(fname, "rb+")
m = mmap(f.fileno(), 0)
size = m.size()
ixl = 0 # Index of line to delete
nle = 0 # Count new lines found
# Find the line:
while nle!=nl:
# Suppose we know our EOL will always be \n
i = m.find("\n", ixl)
if i==-1: break
ixl = i+1
nle += 1
if ixl>=size: f.close(); return # nl is greater than number of lines in the f
ixle = m.find("\n", ixl) # Index of end of that line
ixle = (ixle+1, None)[ixle==-1] #Either include the EOL in deletion or delete to the EOF
# Line length:
if ixle!=None: ll = ixle-ixl
else:
# Remove from ixl to the end of file.
# I.e. just shrink the file.
ns = size-(size-ixl)
if ns==0:
# Delete all
m.close()
f.close()
f = open(fname, "wb")
f.close()
return
m.resize(ns) # Cut off the rubbish
m.close(); f.close()
return
# Shift the rest over the offending line:
try: m[ixl:size-ll] = m[ixle:size]
except:
m.close()
f.close()
raise
ns = size-ll
if ns==0:
# Delete all - mmap doesn't like to resize to 0 bytes., hm, perhaps f.truncate()
m.close()
f.close()
f = open(fname, "wb")
f.close()
return
m.resize(ns) # Cut off the rubbish
m.close()
f.close()