|
10 | 10 | # .txt file |
11 | 11 |
|
12 | 12 | # Leer, escribir y sobrescribir si ya existe |
13 | | -txt_file = open("Intermediate/my_file.txt", "w+") |
| 13 | +txt_file = open("my_file.txt", "w+") |
14 | 14 |
|
15 | 15 | txt_file.write( |
16 | 16 | "Mi nombre es Brais\nMi apellido es Moure\n35 años\nY mi lenguaje preferido es Python") |
17 | 17 |
|
18 | | -# print(txt_file.read()) |
| 18 | +# Posiciona el cursor al inicio del fichero |
| 19 | +txt_file.seek(0) |
| 20 | + |
| 21 | +# Lee e imprime todo el contenido del fichero |
| 22 | +print(txt_file.read()) |
| 23 | + |
| 24 | +# Lee e imprime 10 caracteres desde el inicio del fichero |
| 25 | +txt_file.seek(0) |
19 | 26 | print(txt_file.read(10)) |
| 27 | + |
| 28 | +# Lee e imprime el resto de la línea actual desde la posición 11 |
20 | 29 | print(txt_file.readline()) |
| 30 | + |
| 31 | +# Lee e imprime la siguiente línea |
21 | 32 | print(txt_file.readline()) |
| 33 | + |
| 34 | +# Lee e imprime las líneas restantes del fichero |
22 | 35 | for line in txt_file.readlines(): |
23 | 36 | print(line) |
24 | 37 |
|
| 38 | +# Escribe una nueva línea en el fichero |
25 | 39 | txt_file.write("\nAunque también me gusta Kotlin") |
26 | | -print(txt_file.readline()) |
27 | 40 |
|
| 41 | +# Posiciona el cursor al inicio del fichero, lee e imprime todo su contenido |
| 42 | +txt_file.seek(0) |
| 43 | +print(txt_file.read()) |
| 44 | + |
| 45 | +# Cierra el fichero |
28 | 46 | txt_file.close() |
29 | 47 |
|
30 | | -with open("Intermediate/my_file.txt", "a") as my_other_file: |
| 48 | +# Agrega una nueva línea en el fichero |
| 49 | +with open("my_file.txt", "a") as my_other_file: |
31 | 50 | my_other_file.write("\nY Swift") |
32 | 51 |
|
33 | 52 | # os.remove("Intermediate/my_file.txt") |
|
0 commit comments