1
+ """
2
+ ------------------
3
+ MANEJO DE FICHEROS
4
+ ------------------
5
+ IMPORTANTE: Sólo debes subir el fichero de código como parte del ejercicio.
6
+
7
+ EJERCICIO:
8
+ Desarrolla un programa capaz de crear un archivo que se llame como
9
+ tu usuario de GitHub y tenga la extensión .txt.
10
+ Añade varias líneas en ese fichero:
11
+ - Tu nombre.
12
+ - Edad.
13
+ - Lenguaje de programación favorito.
14
+ Imprime el contenido.
15
+ Borra el fichero.
16
+
17
+ DIFICULTAD EXTRA (opcional):
18
+ Desarrolla un programa de gestión de ventas que almacena sus datos en un
19
+ archivo .txt.
20
+ - Cada producto se guarda en una línea del arhivo de la siguiente manera:
21
+ [nombre_producto], [cantidad_vendida], [precio].
22
+ - Siguiendo ese formato, y mediante terminal, debe permitir añadir, consultar,
23
+ actualizar, eliminar productos y salir.
24
+ - También debe poseer opciones para calcular la venta total y por producto.
25
+ - La opción salir borra el .txt.
26
+ """
27
+ import os
28
+
29
+ # Ejercicio
30
+
31
+ file_name = "SergioGI99.txt"
32
+
33
+ with open (file_name , "w" ) as file :
34
+ file .write ("Sergio Garcia\n " )
35
+ file .write ("24 años\n " )
36
+ file .write ("Python" )
37
+
38
+ with open (file_name , "r" ) as file :
39
+ print (file .read ())
40
+
41
+ os .remove (file_name )
42
+
43
+ # Extra
44
+
45
+ file_name = "sales.txt"
46
+
47
+ open (file_name , "a" )
48
+
49
+ def name_check (name ):
50
+ in_list = False
51
+ with open (file_name , "r" ) as file :
52
+ for line in file .readlines ():
53
+ if line .split (", " )[0 ] == name :
54
+ in_list = True
55
+ if in_list == False :
56
+ print ("No se ha encontrado el producto" )
57
+
58
+
59
+ def check_list ():
60
+ name = input ("Nombre del producto a buscar: " )
61
+ name_check (name )
62
+ with open (file_name , "r" ) as file :
63
+ for line in file .readlines ():
64
+ if line .split (", " )[0 ] == name :
65
+ print (line )
66
+ continue_check ()
67
+
68
+ def add ():
69
+ name = input ("Nombre del producto: " )
70
+ amount = input ("Cantidad vendida: " )
71
+ price = input ("Precio: " )
72
+ with open (file_name , "a" ) as file :
73
+ file .write (f"{ name } , { amount } , { price } \n " )
74
+ print (f"{ name } ha sido añadido." )
75
+ continue_check ()
76
+
77
+ def update ():
78
+ name = input ("Nombre del producto a actualizar: " )
79
+ name_check (name )
80
+ update_value = input ("Que quieres actualizar?\n - Nombre\n - Cantidad\n - Precio" )
81
+ with open (file_name , "r" ) as file :
82
+ lines = file .readlines ()
83
+ with open (file_name , "w" ) as file :
84
+ for line in lines :
85
+ if line .split (", " )[0 ] == name :
86
+ if update_value .lower () == "nombre" :
87
+ new_name = input ("Escribe el nuevo nombre: " )
88
+ amount = line .split (", " )[1 ]
89
+ price = line .split (", " )[2 ]
90
+ file .write (f"{ new_name } , { amount } , { price } \n " )
91
+ elif update_value .lower () == "cantidad" :
92
+ new_amount = input ("Escribe la cantidad vendida: " )
93
+ name = line .split (", " )[0 ]
94
+ price = line .split (", " )[2 ]
95
+ file .write (f"{ name } , { new_amount } , { price } \n " )
96
+ elif update_value .lower () == "precio" :
97
+ new_price = input ("Escribe el precio actualizado: " )
98
+ name = line .split (", " )[1 ]
99
+ amount = line .split (", " )[2 ]
100
+ file .write (f"{ name } , { amount } , { new_price } \n " )
101
+ else :
102
+ print ("Opción incorrecta." )
103
+ update ()
104
+ else :
105
+ file .write (line )
106
+ continue_check ()
107
+
108
+ def delete ():
109
+ name = input ("Nombre del producto a eliminar: " )
110
+ name_check (name )
111
+ with open (file_name , "r" ) as file :
112
+ lines = file .readlines ()
113
+ with open (file_name , "w" ) as file :
114
+ for line in lines :
115
+ if line .split (", " )[0 ] != name :
116
+ file .write (line )
117
+ continue_check ()
118
+
119
+ def show_list ():
120
+ with open (file_name , "r" ) as file :
121
+ print (file .read ())
122
+ continue_check ()
123
+
124
+ def total_sales ():
125
+ total = 0
126
+ with open (file_name , "r" ) as file :
127
+ lines = file .readlines ()
128
+ for line in lines :
129
+ total = total + (int (line .split (", " )[1 ]) * int (line .split (", " )[2 ]))
130
+ print (f"Valor ventas total: { total } " )
131
+ continue_check ()
132
+
133
+ def product_sales ():
134
+ name = input ("Nombre del producto: " )
135
+ name_check (name )
136
+ total = 0
137
+ with open (file_name , "r" ) as file :
138
+ lines = file .readlines ()
139
+ for line in lines :
140
+ if line .split (", " )[0 ] == name :
141
+ total = total + (int (line .split (", " )[1 ]) * int (line .split (", " )[2 ]))
142
+ print (f"Valor ventas total: { total } " )
143
+ continue_check ()
144
+
145
+ def exit ():
146
+ return None
147
+
148
+ def continue_check ():
149
+ input ("(pulsa enter)" )
150
+ sales ()
151
+
152
+ input_list = {
153
+ "1" :check_list ,
154
+ "2" :add ,
155
+ "3" :update ,
156
+ "4" :delete ,
157
+ "5" :show_list ,
158
+ "6" :total_sales ,
159
+ "7" :product_sales ,
160
+ "8" :exit
161
+ }
162
+
163
+ def sales ():
164
+ print ("¿Que operación quieres realizar?\n 1 - Consultar\n 2 - Añadir\n 3 - Actualizar\n 4 - Eliminar\n 5 - Listado\n 6 - Venta total\n 7 - Venta por producto\n 8 - Salir" )
165
+ user_input = input (": " )
166
+ if user_input .lower () in input_list :
167
+ input_list [user_input ]()
168
+ else :
169
+ print (f"{ user_input } no es correcto." )
170
+ continue_check ()
171
+
172
+ sales ()
173
+
174
+ os .remove (file_name )
0 commit comments