With a File
Lección 12 de 28 del curso Clean Code - Write better code using Python de Coddy.
Python usa una palabra reservada especial al trabajar con file: <strong>with</strong>.
Al usar la sintaxis with open, el file que abras se cerrará automáticamente por ti cuando salgas del bloque with.
with open('file.txt') as f:
content = f.read()
...La instrucción
withes mejor porque garantizará que siempre cierres el file, incluso si se produce una excepción dentro del bloquewith.
Pruébalo tú mismo
Esta lección no incluye un desafío de código.
Todas las lecciones de Clean Code - Write better code using Python
4Pythonic Code
The ZenVariable TricksDealing With ListsWith a FileAccess a Dictionary ElementEquals OperationsPractica por tu cuenta: Compilador de Python online