Re: Changing a line in a text file

2005-04-26 Thread Simon Brunning
On 4/25/05, Kirk Job Sluder <[EMAIL PROTECTED]> wrote: > In addition, I would argue that editing a file in place using a > non-interactive program is dangerous and bad practice in general. By > the time you find a bug in your edit script, the original is lost. This > is something I learned from b

Re: Changing a line in a text file

2005-04-25 Thread Kirk Job Sluder
Steve Holden <[EMAIL PROTECTED]> writes: > kah wrote: > However, you asked about replacing one line with another of a > different length: since this will mean changing the offsets of all > subsequent bytes you have no way to do this other than writing out the > whole content of the file following

Re: Changing a line in a text file

2005-04-25 Thread Larry Bates
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.wr

Re: Changing a line in a text file

2005-04-25 Thread Miki Tebeka
Hello kah, > 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. See http://docs.python.org/lib/module-fileinput.h

Re: Changing a line in a text file

2005-04-25 Thread Steve Holden
kah wrote: Hi, the example provided by Vishnu is quite close to what I want but it still required me to write all the data back to the file. Is there any way where I can just write a particular line? If you are asking whether you can update a file in place, the answer is "yes" - look up "lseek" in

Re: Changing a line in a text file

2005-04-25 Thread kah
Hi, the example provided by Vishnu is quite close to what I want but it still required me to write all the data back to the file. Is there any way where I can just write a particular line? Regards, Kah -- http://mail.python.org/mailman/listinfo/python-list

RE: Changing a line in a text file

2005-04-25 Thread Vishnu
H, Vishnu -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of kah Sent: Monday, April 25, 2005 3:09 PM To: python-list@python.org Subject: Changing a line in a text file How do I change a line in a file?? For example I have the follwing text in my file: lin

Re: Changing a line in a text file

2005-04-25 Thread Michael Hoffman
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. This is the best I can figure out what you mean: lines = [] for line in fil

Changing a line in a text file

2005-04-25 Thread kah
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