|
|
import os |
|
|
import sys |
|
|
|
|
|
def remplacer_recursif(): |
|
|
|
|
|
dossier_racine = os.getcwd() |
|
|
nom_du_script = os.path.basename(__file__) |
|
|
|
|
|
print(f"--- Remplacement Récursif (Dossiers et sous-dossiers) ---") |
|
|
print(f"Racine : {dossier_racine}") |
|
|
|
|
|
ancien_mot = input("Entrez le mot à remplacer : ") |
|
|
nouveau_mot = input("Entrez le nouveau mot : ") |
|
|
|
|
|
if not ancien_mot: |
|
|
print("Erreur : Le mot à remplacer est vide.") |
|
|
return |
|
|
|
|
|
compteur_fichiers = 0 |
|
|
dossiers_ignores = {'.git', '.idea', '__pycache__', 'venv', 'node_modules'} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for racine, dossiers, fichiers in os.walk(dossier_racine): |
|
|
|
|
|
|
|
|
|
|
|
dossiers[:] = [d for d in dossiers if d not in dossiers_ignores] |
|
|
|
|
|
for fichier in fichiers: |
|
|
chemin_complet = os.path.join(racine, fichier) |
|
|
|
|
|
|
|
|
if fichier == nom_du_script and racine == dossier_racine: |
|
|
continue |
|
|
|
|
|
try: |
|
|
|
|
|
with open(chemin_complet, 'r', encoding='utf-8') as f: |
|
|
contenu = f.read() |
|
|
|
|
|
|
|
|
if ancien_mot in contenu: |
|
|
nouveau_contenu = contenu.replace(ancien_mot, nouveau_mot) |
|
|
|
|
|
|
|
|
with open(chemin_complet, 'w', encoding='utf-8') as f: |
|
|
f.write(nouveau_contenu) |
|
|
|
|
|
print(f"[MODIFIÉ] {chemin_complet}") |
|
|
compteur_fichiers += 1 |
|
|
|
|
|
except UnicodeDecodeError: |
|
|
|
|
|
pass |
|
|
except Exception as e: |
|
|
print(f"[ERREUR] Sur {chemin_complet} : {e}") |
|
|
|
|
|
print(f"\nTerminé ! {compteur_fichiers} fichier(s) modifiés au total.") |
|
|
|
|
|
if __name__ == "__main__": |
|
|
remplacer_recursif() |
|
|
|