Files, Exceptions & Context Managers
Unit 4: Files, Exceptions & Standard Library · 8 min read
Reading and parsing text safely with pathlib and UTF-8 encoding, clean try/except/else/finally blocks, and custom exception classes.
Key Concepts
- EAFP (Easier to Ask for Forgiveness than Permission)
- Always specify encoding='utf-8' when opening text
- Never use bare except: (catches KeyboardInterrupt/SystemExit)
- Exception chaining with raise CustomError(...) from err