filename = "foo"
f = open(filename, 'r+')
s = f.read().strip()
s_splitlinenums = s.split("\n") [1, 3]
# Remove lines 1 and 3 s = [y for x, y in enumerate(f) if x not in [line-1 for
linenumsline =in range(1,3)
forlinenums]] linenum in linenums:
s_split.pop(linenum-1)
f.seek(0)
f.write('\n'''.join(s_splits))
f.truncate(f.tell())
f.close()
BTW, sometimes the Python docs really suck. See http://docs.python.org/library/functions.html#openSome notes:
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"?
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
me to
Sometimes the Python docs really
suck. See
http://docs.python.org/library/functions.html#open
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.