Line replace

Steven D'Aprano steve at REMOVETHIScyber.com.au
Sun Jan 1 07:03:20 EST 2006


On Sun, 01 Jan 2006 19:31:38 +0800, DarkBlue wrote:

> Hello
> 
>   I need some help
> 
>   I have a text file which changes dynamically and has
>   200-1800 lines. I need to replace a line , this line
>   can be located via a text marker like :
> 
>   somelines
>   # THIS IS MY MARKER
>   This is the line to be replaced
>   somemorelines
>   
>   My question is how to do this in place without
>   a temporary file , so that the line after
>   the marker is being replaced with mynewlinetext.

Let me see if I understand your problem... you need to edit a text file
in place at the same time that another process is also changing the file
in place? That's hard. You need some way to decide who gets precedence if
both you and the other process both try to change the same line
simultaneously.

I think the only way this is even half-doable will be if:

- the other process writing to the file only appends to the end of the
file, and does not try to write to the middle;

- the new line you are writing is the same length as the old line you are
replacing; 

- and you are running an operating system that allows two processes to
have simultaneous write access to a file.

What problem are you trying to solve by having simultaneous writes to the
same file? Perhaps there is another way.


-- 
Steven.




More information about the Python-list mailing list