Search and replace a specific word in a file object.
Michel Claveau/Hamster
No.Spam.mc at No.Spam.mclaveau.No.Spam.com
Sun Jan 11 18:40:05 EST 2004
Hi !
You can try this :
def remplacerdansfichiertxt(file, listeremplacement, filout):
# remplacement dans un fichier texte (et non binary)
import os,os.path,re
if os.path.isdir(file):
print(file+' is a Directory !')
else:
data = open(file, "rb").read()
if '\0' in data:
print('it's a binary file')
else:
newdata=data
for chaine,remplacement in listeremplacement :
newdata = re.sub(chaine, remplacement, newdata)
if newdata != data:
f = open(filout, "wb")
f.write(newdata)
f.close()
print(file,';',chaine,' => ',remplacement,':',filout,' :
OK')
in the call, the parameter "listeremplacement" is like
[('oldVal','newVal'),('aaa','bbbb'),('qwerty','azerty')]
@-salutations
--
Michel Claveau
More information about the Python-list
mailing list