1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
| from os import makedirs
from os.path import isdir
from shutil import rmtree
#if isdir('C:\\ESSAIC\\dedans\\'):
# rmtree('C:\\ESSAIC\\dedans\\')
#if isdir('C:\\ESSAIC\\'):
# rmtree('C:\\ESSAIC\\')
makedirs('C:\\ESSAIC\\dedans\\')
for fnm in ('C:\\ESSAIC\\text un.c.txt',
'C:\\ESSAIC\\texte deux.c.txt'):
fu = open(fnm,'w')
fu.write('le rURDUomain fend,URDU\n'
'soleil :URDU>> \fURDU URDU URDU\n'
'URDUPloyer et\tURDU imperator romURDUain\n'
'prunelles uRDU claires ;\n'
'le romURDUain sentait\n'
'Le romaiURDUn est un romURDUain')
fu.close()
fu = open('C:\\ESSAIC\\dedans\\wqer.c.txt','w')
fu.write('il URDU\nle \fURDU URDU URDU\n'
'et\tURDU imperator \nprunelles uRDU claires')
fu.close()
fu = open('C:\\ESSAIC\\dedans\\derty.c.txt','w')
fu.write('hhfhfhfhgf kjhkgkgkg\nabcfgedfssubcd')
fu.close()
fu = open('C:\\ESSAIC\\dedans\\vide.c.txt','w')
fu.close()
###################################################
from os import fsync,walk,path
import re
def searchandreplace(x, y, setpath):
minRE = '(?<=\s)'+x+'(?=\s)'
pat_others = re.compile('\s[^\s]+'+x+'[^\s]*\s|\s'+x+'[^\s]+\s')
for root, dirs, files in walk(setpath):
print 8*'---------'+'\n'\
+'root = %r\ndirs = %r\nfiles = %r\n' % (root,dirs,files)
gen_fname = (fn for fn in files
if ('.c' in fn or '.h' in fn) and not '.csv' in fn )
for fname in gen_fname:
print '-*- Fichier '+fname+ ' ----------------------'
inputFile = open(path.join(root,fname), 'r+')
data = inputFile.read()
free_chains = re.search(minRE,data)
autres = set(pat_others.findall(data))
if autres:
cles = map(str,xrange(1,len(autres)+1))
deek_autres = dict(zip(cles,autres))
bla = "\nJ'ai trouve des occurences de "+x+" a l'interieur de mots.\n"\
"Indiquer, au moyen de leur numero, celles que vous voulez substituer aussi:\n"\
" 0 : ~ AUCUNE AUTRE ~\n"\
+'\n'.join( num.rjust(4)+' : '+repr(deek_autres[num]) for num in sorted(deek_autres.keys()))\
+"\necrire les nos separes par des blancs : "
while 1:
linums = raw_input(bla).split()
if not linums:
bla = "\nUne reponse est demandee. Recommencez:\n ==> : "
elif '0' in linums and any(z!='0' for z in linums):
bla = "\nVous ne pouvez indiquer un autre nombre en sus de 0."\
" Recommencez:\n ==> : "
elif '0' not in linums and any(w not in cles for w in linums):
bla = "\nSeuls les nombres 0 a " + str(len(autres)+1)\
+ " sont autorises. Recommencez:\n ==> : "
elif len(set(linums))!=len(linums):
bla = "\nVeuillez entrez des nombres tous differents."\
" Recommencez:\n ==> : "
else:
break
linums.sort()
if linums!=['0']: # on remplace les x libres et d'autres
RENCORE = '|'.join("(?<="+((')'+x+'(?=').join(deek_autres[num].split(x)))+")"
for num in linums )
data = re.sub(minRE + '|' + RENCORE,y,data)
done = "\nHaving replaced with '" + y + "' in " + fname + ' :\n'\
"free '"+x+"' chains , "\
+ ' , '.join("'"+deek_autres[num]+"'" for num in linums)
elif free_chains: # on ne remplace que les x libres
data = re.sub(minRE,y,data)
done = "\nHaving replaced only free '"+x+"' chains with '" + y + "' in " + fname + '\n'
else:
done = "\nHaving replaced no occurence of '" + x + "'\n"\
"There was no occurence of free '" + x + " chain to replace in " + fname + '\n'
elif free_chains:
data = re.sub(minRE,y,data)
done = "\nHaving replaced free '"+x+"' chains with '" + y + "' in " + fname + '\n'
elif not path.getsize(root+'\\'+fname):
print "\nNo replacement to do: this file is empty.\n"
done = ''
else:
print "\nNo replacement to do: this file isn't empty,"\
"\nbut there is no occurence of '"+x+"' to replace in it.\n"
done = ''
if done:
inputFile.seek(0)
inputFile.write(data)
inputFile.flush()
fsync(inputFile.fileno())
print done
inputFile.close()
setpath = 'C:\\ESSAIC\\'
searchandreplace('URDU', "AGLAGLA", setpath)
########################################################################
print 8*'#########'
for fnm in ('C:\\ESSAIC\\text un.c.txt',
'C:\\ESSAIC\\texte deux.c.txt',
'C:\\ESSAIC\\dedans\\wqer.c.txt',
'C:\\ESSAIC\\dedans\\derty.c.txt',
'C:\\ESSAIC\\dedans\\vide.c.txt'):
print '\n-*- '+fnm
if not path.getsize(fnm):
print ' Ce fichier est vide.'
else:
fu = open(fnm)
print '\n'.join(repr(ligne) for ligne in fu.readlines())
fu.close()
rmtree('C:\\ESSAIC\\dedans\\')
rmtree('C:\\ESSAIC\\') |
Partager