File tree Expand file tree Collapse file tree 3 files changed +42
-0
lines changed
06_archivos_y_modulos/autonomos/ejercicio5 Expand file tree Collapse file tree 3 files changed +42
-0
lines changed Original file line number Diff line number Diff line change 1+ ## Ejercicio Autónomo Nº 5
2+
3+ Pide por ` input() ` una ruta de archivo y valida si existe antes de leer.
Original file line number Diff line number Diff line change 1+ # utiles.py
2+ # Módulo donde se almacena funciones útiles para leer y escribir archivos
3+
4+ # TODO 1: Función que lee el contenido del archivo y las líneas de texto que contiene
5+ def leer_lineas (ruta ):
6+ try :
7+ print ("Leyendo archivo... \n " )
8+ with open (ruta , 'r' , encoding = "utf-8" ) as archivo :
9+
10+ contenido = archivo .read ()
11+ print (contenido )
12+
13+ print ("\n Contando número de líneas:" )
14+ with open (ruta , 'r' , encoding = "utf-8" ) as archivo :
15+
16+ lineas = archivo .readlines ()
17+ print (len (lineas ) - 1 )
18+
19+ print (f"Archivo { ruta } corregido con éxito ✅" )
20+
21+ except FileNotFoundError :
22+ print ("❌ Archivo no encontrado.\n " )
23+ except Exception as e :
24+ print (f"\n ❌ Error al leer el archivo: { e } " )
25+
26+ # TODO 2: Función para guardar las líneas a un archivo específico
27+ def guardar_lineas (ruta , lineas ):
28+ try :
29+ print ("Escribiendo archivo...\n " )
30+ with open (ruta , 'w' , encoding = "utf-8" ) as archivo :
31+
32+ for linea in lineas :
33+ archivo .write (f"\n { linea } " )
34+
35+ print (f"Archivo { ruta } creado con éxito ✅" )
36+ except FileNotFoundError :
37+ print ("❌ Archivo no encontrado.\n " )
38+ except Exception as e :
39+ print (f"\n ❌ Error al escribir el archivo: { e } " )
You can’t perform that action at this time.
0 commit comments