Replace and inserting strings within .txt files with the use of regex

rantingrick rantingrick at
Sun Aug 8 02:40:33 CEST 2010

On Aug 7, 7:20 pm, Νίκος < at> wrote:
> Hello dear Pythoneers,

I prefer Pythonista, but anywho..

> I have over 500 .php web pages in various subfolders under 'data'
> folder that i have to rename to .html

import os
os.rename(old, new)

> and and ditch the '<?' and '?>' tages from within

path = 'some/valid/path'
f = open(path, 'r')
data =
data.replace('<?', '')
data.replace('?>', '')

>  and also insert a very first line of <!-- id -->
> where id must be an identification unique number of every page for
> counter tracking purposes.

comment = "<!-- %s -->"%(idnum)
data.insert(idx, comment)

> ONly pure html code must be left.

Well then don't F up! However judging from the amount of typos in this
post i would suggest you do some major testing!

> I don't know how to handle such a big data replacing problem and
> cannot play with fire because those 500 pages are my cleints pages and
> data of those files just cannot be messes up.

Better do some serous testing first, or (if you have enough disc
space ) create copies instead!

> Can you provide to me a script please that is able of performing an
> automatic way of such a page content replacing?

This is very basic stuff and the fine manual is free you know. But how
much are you willing to pay?

More information about the Python-list mailing list