Replace parts of a file?

François Granger francois.granger at free.fr
Sun Oct 1 09:39:41 EDT 2000


Gustaf Liljegren <gustafl at algonet.se> wrote:

> Here's a newbie question. I'm working with a script for replacing the
> header and footer sections of multiple HTML files. I don't think I can use
> SSI here, as I need to change some file paths in these sections too.
> 
> For example, the footer (in this case) is everything below the last "<hr>".
> Below that line, different files may look a slightly different. After the
> "<hr>", I want to remove everything and replace it with a standardized
> footer from another file. I came this far:
> 
>   file = open(filename, "r+")
>   start = string.rfind(file, "<hr>")

What about (not tested)
file = open(filename, r)
str = file.read()
start = string.rfind(str, '<hr>')
str = str[:start] + your_new_footer

#write this str in file here

-- 
"Faites des phrases courtes. Un sujet, un verbe, un complément. Quand
vous voudrez ajouter un adjectif, vous viendrez me voir." - Georges
Clemenceau, 1841-1929. Consignes aux journalistes de "L'Aurore".



More information about the Python-list mailing list