Using Regular Expresions to change .htm to .php in files

Tim Williams tdwdotnet at gmail.com
Thu Aug 23 16:10:26 EDT 2007


On 23/08/07, Tim Williams <tdwdotnet at gmail.com> wrote:
> On 23/08/07, sebzzz at gmail.com <sebzzz at gmail.com> wrote:
> > Hi,
> >
> > I have a bunch of files that have changed from standard htm files to
> > php files but all the links inside the site are now broken because
> > they point to the .htm files while they are now .php files.
> >
> > Does anyone have an idea about how to do a simple script that changes
> > each .htm in a given file to a .php
> >
> > Thanks a lot in advance
> >
>
> Something like:
>
> Infile = open(f_name,'r+')
> Data =  Infile.read()
> InFile.write(Data.replace('.htm','.php'))
> Infile.close()
>

Actually,  scrub that, its been a long day!    You either need a
Infile.seek(0)  in the above before the write() , or you can do.

Data = open(f_name).read()
outfile = open(f_name,'w')
outfile.write(Data.replace('.htm','.php'))
outfile.close()



More information about the Python-list mailing list