Course Content
Python Essentials for AI Engineer
6 sections · 48 lessons
What is the recommended way to open files?
What you need to know
What goes wrong without with
An open file is an operating-system resource (a file handle). Written data sits in a memory buffer until the file is flushed or closed. If you forget close(), or an exception skips it:
- Data can be lost when the process exits abruptly.
- Handles pile up; the OS limit is often 256 on macOS and 1,024 on Linux, after which you get
OSError: [Errno 24] Too many open files. - On Windows, an open file cannot be deleted or renamed by other programs.
What with does
with open("data.txt", "w", encoding="utf-8") as f: f.write("hello\n")print(f.closed) # True -> closed as soon as the block endedIt is shorthand for this try/finally:
1f = open("data.txt", encoding="utf-8")2try:3 text = f.read()4finally:5 f.close() # runs on success, on exception, and on return6print(text.strip()) # hellowith works with any context manager — an object with __enter__ and __exit__ methods. Locks, database connections and HTTP sessions use the same pattern. You can open several files in one statement: with open(src) as fin, open(dst, "w") as fout:.
Why pass the encoding
A text file is just bytes; the encoding is the rule that turns bytes into characters. UTF-8 can represent every language. But when you leave encoding out, Python uses the platform's locale encoding, which on many Windows machines is cp1252. Python 3.15 plans to make UTF-8 the default everywhere (PEP 686), but code running on 3.11–3.14 still needs it spelled out.
1raw = "नमस्ते".encode("utf-8") # how a UTF-8 file stores it2try:3 raw.decode("cp1252") # what a Windows default might do4except UnicodeDecodeError as e:5 print("UnicodeDecodeError") # UnicodeDecodeError6print("café".encode("utf-8").decode("cp1252")) # café -> silent garbage, even worseA real-life example
A team builds a multilingual FAQ bot with Hindi, Tamil and English answers stored in UTF-8 text files. Everything works on the Linux build server. A new teammate runs the ingestion script on a Windows laptop, where one file crashes with UnicodeDecodeError and others load as garbled text like café — which then gets embedded and indexed. Retrieval quality for non-English questions quietly drops.
The fix is one argument on every open() call: encoding="utf-8". Many teams also turn on the linter rule that flags open() without an encoding (Pylint's unspecified-encoding), so it never happens again.
Follow-up questions to expect
- "What does
withdo under the hood?" — It calls__enter__at the start and guarantees__exit__at the end, which for files callsclose(). - "If an exception happens inside
with, is it swallowed?" — No. The file is closed, and the exception keeps propagating (a file's__exit__does not suppress it). - "
open()orpathlib?" —pathlib.Pathis the modern way to build and inspect paths; itsread_text/write_textare handy for small files, andpath.open()works withwithfor streaming.