Archivos y datos
Hasta ahora todo lo que hiciste se perdía al cerrar la pestaña. Los archivos son la memoria permanente de tu programa.
Files and data
Everything you have done so far vanished when the tab closed. Files are your program's permanent memory.
open() abre un archivo. Necesita el nombre y el modo:
"w" para escribir (borra lo que hubiera antes), "r" para leer,
"a" para agregar al final sin borrar.
Se usa junto a with, que se encarga de cerrar el archivo solo al terminar
—incluso si algo falla a mitad de camino.
open() opens a file. It needs the name and the mode:
"w" to write (erases whatever was there), "r" to read,
"a" to append without erasing.
It is used together with with, which closes the file automatically when
done — even if something fails halfway through.
Para leer, cambia el modo a "r". Hay dos formas típicas: leer todo de una
vez con .read(), o recorrer línea por línea con un for — la misma
idea que usaste con listas.
To read, switch the mode to "r". Two common ways: read it all at once with
.read(), or loop through it line by line with a for — the same idea
you used with lists.
El .strip() le quita el salto de línea sobrante al
final — el mismo método que viste en el tutorial de textos.
The .strip() removes the trailing line break — the
same method from the text tutorial.
El modo "w" borra el archivo cada vez que lo abres. Si
quieres sumar contenido sin perder lo anterior, usa "a" —de append—.
Es exactamente cómo funciona un archivo de registro (log): cada vez que pasa algo, se agrega una línea nueva al final.
The "w" mode erases the file every time you open it. To add
content without losing what was there, use "a" — for append.
It is exactly how a log file works: every time something happens, a new line gets added at the end.
Acá se junta todo lo que sabes: archivos, .split() y listas. Este es
justamente el formato en que se guarda una planilla de Excel como texto plano.
Here everything comes together: files, .split() and lists. This is exactly
the format a spreadsheet is saved as when exported as plain text.
open() con sus modos "r", "w" y "a",
with para cerrar solo, leer línea por línea, y procesar un archivo tipo CSV.
Ahora que sabes tocar archivos, el siguiente paso es hacer que Python trabaje solo con muchos de ellos a la vez.
open() with its "r", "w" and "a"
modes, with to close automatically, reading line by line, and processing a
CSV-style file.
Now that you can touch files, the next step is making Python work on its own with many of them at once.