Just read it into memory, alter it, then write it back. You can do something like
filename = "foo"
f = open(filename, 'r+')
linenums = [1, 3]
s = [y for x, y in enumerate(f) if x not in [line-1 for line in linenums]]
Tested with a 5 line file. Credits to http://pleac.sourceforge.net/pleac_python/fileaccess.html, see section "Modifying a File in Place Without a Temporary File". See also http://stackoverflow.com/questions/125703/how-do-i-modify-a-text-file-in-python
One could first truncate the file,
then write to it, rather than write,
then truncate, as above. However, I
don't know of a Python flag that
allows one to read, and then do a
truncated write. But maybe I'm
missing something, as the document
isn't all that clear. Which brings
Sometimes the Python docs really
Modes 'r+', 'w+' and 'a+' open the file for updating (note that 'w+'
truncates the file).
Does this mean anything to you? What
the hell is "open for updating"?
I don't know if doing this in python
as opposed to something unixy like
the stream editor is better. It
might be more portable, but I don't
know how portable sed is. I just
wrote it like that because I'm more
comfortable with low level
programming than using classic unix
tools, which are good if they do
exactly what you want, but (I think)
are generally less flexible.
This approach (manipulating the file in memory)
trades memory for disk
space. It should work Ok on machines
with a few Gb of memory for files up
to a few hundred Mb. Python doesn't
handle strings very efficiently, so
switching to C/C++ for example would
slightly increase performance and
greatly reduce memory usage.