Voici un de ces script qui ne servent qu'une fois... mais bon peut être celui-ci peut vous servir. Je l'ai écrit pour inserer un code google analytics sur un site avec 77 pages html statiques.


#!/usr/bin/env python # AUTHOR : Philippe Mironov # LICENCE : GPLv2+ #inserts tag_to_insert before tag_before in every file in CWD in ext # checks if tag_to_insert and tag_check are not already in file # skips files without tag_before in them. # Can be used to add insert statements or for exemple google analytics code in an old static website. ext = "html" tag_to_insert = """""" tag_before = "" tag_check = "*******" import os print 'CWD is : ', os.getcwd() for item in os.listdir(os.getcwd()): if item.endswith(ext) : #item is an html file #check if it's not already tagged fo = open(item,'r') buf_orig = fo.read() fo.close() if not buf_orig.find(tag_check) == -1: print "%s is marked by tag_check. SKIPPING" % (item) continue if not buf_orig.find(tag_to_insert) == -1: print "%s has already the tag inserted. SKIPPING" % (item) continue offset = buf_orig.find(tag_before) if offset == -1 : print "%s has no %s. SKIPPING" % (item, tag_before) continue fo = open(item,'w') fo.write(buf_orig[:offset] + tag_to_insert + buf_orig[offset:]) fo.close() print "Inserted tag into ", item