File tree 1 file changed +55
-0
lines changed
Roadmap/11 - MANEJO DE FICHEROS/python
1 file changed +55
-0
lines changed Original file line number Diff line number Diff line change
1
+ #Importamos el modulo os para trabajar con el sistema
2
+ import os
3
+
4
+ #Función que crea un fichero con la extensión .txt
5
+ def create_file ():
6
+ #Abrimos el fichero, y le indicamos el nombre con el que se guardara
7
+ file = open ("mariovelascodev.txt" , "w" )
8
+
9
+ #Solicitamos que el usuario nos introduca su nombre, edad, y lenguaje de programación favorito
10
+ add_data = []
11
+ name = input ("Introduce tu nombre: " )
12
+ age = input ("Introduce tu edad: " )
13
+
14
+ #Añadimos lo datos introducidos por el usuario a una lista
15
+ add_data .append (name )
16
+ add_data .append (age )
17
+
18
+ #Solicitamos tantos lenguajes de programción como quiera añadir el usuario
19
+ while True :
20
+ favorite_language = input ("Introduce tu lenguaje de programación favorito(escribe \" q\" para salir): " )
21
+ if favorite_language == "q" :
22
+ break
23
+ else :
24
+ add_data .append (favorite_language )
25
+
26
+ #Recorremos la lista y escribimos los valores en lineas distintas del fichero
27
+ for line in add_data :
28
+ file .write (f"{ line .title ()} \n " )
29
+
30
+ #Cerramos el fichero
31
+ file .close ()
32
+
33
+ #Función que lee un fihero
34
+ def read_file ():
35
+ #Abrimos el fichero y lo leemos
36
+ file = open ("mariovelascodev.txt" , "r" )
37
+
38
+ print ("\n El contenido del fichero es:" )
39
+ print (file .read ())
40
+
41
+ #Cerramos el fichero
42
+ file .close ()
43
+
44
+ #Función que elimina un fichero
45
+ def remove_file ():
46
+ #Nombre del fichero a eliminar
47
+ file = "mariovelascodev.txt"
48
+
49
+ #Borrado del fichero indicado
50
+ os .remove (file )
51
+
52
+
53
+ create_file ()
54
+ read_file ()
55
+ remove_file ()
You can’t perform that action at this time.
0 commit comments