Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- """
- Creado con Spyder
- Busca/Reemplaza texto o patrón en un archivo de texto.
- """
- from pathlib import Path
- def busca_reemplaza(archivo: str, busca: str, reemplaza: str):
- """
- Parameters.
- ----------
- archivo: "Nombre/del/archivo/archivo.txt"
- Cualquier archivo tipo texto.
- busca: str
- Texto o patrón a buscar.
- Sí la palabra a buscar/reemplazar contiene apostrofe,
- usar entrecomillado doble.
- Ejemplo de uso:
- busca_reemplaza("archivo.txt", "you'll", "you will")
- reemplaza : str
- Texto o patrón a buscar.
- El archivo a modificar debe estar en mismo directorio donde se ejecute
- la función. Si no, reporta:
- FileNotFoundError: [Errno 2] No such file or directory: 'archivo.txt'.
- Returns
- -------
- None.
- """
- path = Path(archivo)
- text = path.read_text(encoding='utf-8')
- text = text.replace(busca, reemplaza)
- path.write_text(text, encoding='utf-8')
- # busca_reemplaza("archivo.txt", "you'll", "you will") <-- Ejemplo de uso
Add Comment
Please, Sign In to add comment