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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
| 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\\texte un.c.txt',
'C:\\ESSAIC\\texte deux.c.txt'):
fu = open(fnm,'w')
texte12 = 'URDUgle fend,URDU\n'\
'soleil :URDU>> \fURDU URDU URDU\n'\
'le rURDUoi voile rURDUoi rURDUoi\n'\
'URDUPloyer et\tURDU fonte\n'\
'prunelles uRDU claires ;URDU\r\n'\
'\trURDUoi rURDUoi sel rURDUoi\n'\
'Le rURDUomain est un hyURDU'
fu.write(texte12)
fu.close()
fu = open('C:\\ESSAIC\\texte trois.c.txt','w')
texte3 = 'URDU--* \fURDU URDU papyrus *--URDU'
fu.write(texte3)
fu.close()
fu = open('C:\\ESSAIC\\texte quatre.c.txt','w')
texte4 = 'URDU \r\nURDU URDU palmierURDU URDU'
fu.write(texte4)
fu.close()
fu = open('C:\\ESSAIC\\dedans\\wqer.c.txt','w')
textewq = 'il URDU\nle \fURDU URDU URDU\n'\
'et\tURDU glycine \npois uRDU claires'
fu.write(textewq)
fu.close()
fu = open('C:\\ESSAIC\\dedans\\derty.c.txt','w')
texteko = 'hhfhfhfhgf kjhkgkgkg\nabcfgedfssubcd'
fu.write(texteko)
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):
pat_xfree = re.compile('((?<=\s)|(?<=\A))'+x+'((?=\s)|(?=\Z))')
pat_xfree_and_mots = re.compile('(?:\s|\A)([^\s]*)'+x+'([^\s]*)')
pat_x = re.compile(x)
for root, dirs, files in walk(setpath):
print (8*'---------')+\
'\nroot = %r'\
'\ndirs = %r'\
'\nfiles = %r\n' % (root,dirs,files)
for fname in files:
if not('.c' in fname or '.h' in fname) or '.csv' in fname :
continue
print '-*- Fichier '+fname+ ' ----------------------'
inputFile = open(path.join(root,fname), 'r+')
data = inputFile.read()
free_chains, deek_str, affiche, deek_pos, cnt = [],{},[],{},0
for ma in pat_xfree_and_mots.finditer(data):
titch = ma.group()
ending = ma.end()
if ending!=len(data): # on ajoute le caractere suivant si existe
titch = titch + data[ending]
if ma.group(1)==''==ma.group(2):
free_chains.append(ma.end(1))
elif titch not in deek_pos:
cnt += 1
deek_str[cnt] = titch
affiche.append(str(cnt).rjust(4) + ' : ' + repr(titch))
deek_pos[cnt] = [ma.end(1)]
else:
deek_pos[cnt].append(ma.end(1))
if cnt: del ending,titch,ma
if deek_pos:
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( affiche )\
+"\necrire les nos separes par des blancs : "
while 1:
linums = map(int,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(num not in deek_str for num in linums):
bla = "\nSeuls les nombres 0 a " + str(cnt)\
+ " sont autorises. Recommencez:\n ==> : "
elif len(set(linums))!=len(linums):
bla = "\nVeuillez entrez des nombres tous differents."\
" Recommencez:\n ==> : "
else:
break
if linums!=[0]: # on remplace les x libres et d'autres
target = sum((li for (num,li) in deek_pos.iteritems()
if num in linums ), []) + free_chains
def do(match, to_replace = target):
if match.start() in to_replace: return y
else: return x
data = pat_x.sub(do,data)
done = "\nHaving replaced with %r in %s:\n"\
"free %r chains and %r in :\n%s\n"\
% (y,fname,x,x,
' , '.join(repr(deek_str[num]) for num in linums))
elif free_chains: # on ne remplace que les x libres
data = pat_xfree.sub(y,data)
done = "\nHaving replaced only free %r chains with %r in %s\n"\
% (x,y,fname)
else:
done = "\nHaving replaced no occurence of %r.\n"\
"There was no occurence of free %r chain to replace in %s\n"\
% (x,x,fname)
elif free_chains:
data = pat_xfree.sub(y,data)
done = "\nHaving replaced free %r chains with %r in %s.\n"\
"There is no %r chain in any longer chain in this file.\n"\
% (x,y,fname,x)
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 %r to replace in it.\n"\
%(x,)
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)
########################################################################
for fnm,texte in (('C:\\ESSAIC\\texte un.c.txt',texte12),
('C:\\ESSAIC\\texte deux.c.txt',texte12),
('C:\\ESSAIC\\texte trois.c.txt',texte3),
('C:\\ESSAIC\\texte quatre.c.txt',texte4),
('C:\\ESSAIC\\dedans\\wqer.c.txt',textewq),
('C:\\ESSAIC\\dedans\\derty.c.txt',texteko),
('C:\\ESSAIC\\dedans\\vide.c.txt','')):
print 8*'========='+'\n-*- '+fnm
if not path.getsize(fnm):
print ' Ce fichier est vide.'
else:
fu = open(fnm)
print 'AVANT:\n'\
+'\n'.join(repr(lulu) for lulu in texte.splitlines(1))\
+'\nAPRES:\n'\
+'\n'.join(repr(ligne) for ligne in fu.readlines())
fu.close()
rmtree('C:\\ESSAIC\\dedans\\') |
Partager