edit text in a file and save it again

Gabriel Genellina gagsl-py at yahoo.com.ar
Sun Nov 19 22:28:34 EST 2006


At Sunday 19/11/2006 20:07, cyberco wrote:

>I must be overlooking something here... I'm trying to edit a line in a
>text file. I thought this was easy with fileinput, but all examples do
>not write the line back to the file but simply 'print' it. I want to
>open the file, edit the line and save it again. Is fileinput the right
>module for that?

Short answer: You can't.
Use an intermediate file. Keep reading from the original file and 
writing to the intermediate file, until you find the line to replace. 
Process that line and write to the output; then keep reading and 
writing until the end.
Delete or rename the original file; and rename the intermediate file.


-- 
Gabriel Genellina
Softlab SRL 

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis! 
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar



More information about the Python-list mailing list