donnek
03-29-2001, 10:04 AM
Hi! Can anyone point me in the right direction of a script (in Perl?) that would allow me to replace a fairly large chunk of HTML on a server with another fairly large chunk? I then need to do the same locally on my friend's Win PC, but that's easier.
Thanks.
jemfinch
03-29-2001, 02:47 PM
This should work, but I haven't tested it. Change the appropriate variables, make a backup directory, and try it out on that.
#!/usr/bin/env python
import os, string
basedir = 'change_this'
old = 'oldstuff'
new = 'newstuff'
def visit(arg, dirname, names):
files = filter(lambda x: x[-5:] == '.html', os.listdir(dirname))
files = map(lambda x d=dirname: os.path.join(d, x), files)
for file in files:
fd = open(file, 'r')
content = fd.read()
fd.close()
newcontent = string.replace(content, old, new)
fd = open(file, 'w')
fd.write(newcontent)
fd.close()
os.path.walk(basedir, visit, None)
Yes, I know it's not in perl. But that's only because perl sux0rs.
Jeremy