You might be able to use "edge" case to make this simple.

1) If the line you are replacing is unique in the file

and

2) File can easily fit in memory

you can write:

fp=open(filename, 'r')
contents=fp.read()
fp.close()
newcontents=newline.join(contents.split(line2))
fp=open(filename, 'w')
fp.write(newcontents)
fp.close()

For small files this is very efficient.

Larry Bates


kah wrote:
> How do I change a line in a file??
> 
> For example I have the follwing text in my file:
> 
> line1
> line2
> line3
> line4
> 
> How do I replace 'line2' with 'newline'. Since the write operation in
> python will  overwrite everything.
> 
> Regards,
> Kah
> 
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to