Python Essentials for AI Engineer

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

Python
with open("data.txt", "w", encoding="utf-8") as f:    f.write("hello\n")print(f.closed)          # True -> closed as soon as the block ended

It is shorthand for this try/finally:

Python
f = open("data.txt", encoding="utf-8")try:    text = f.read()finally:    f.close()            # runs on success, on exception, and on returnprint(text.strip())      # hello

with 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.

Python
raw = "नमस्ते".encode("utf-8")             # how a UTF-8 file stores ittry:    raw.decode("cp1252")                     # what a Windows default might doexcept UnicodeDecodeError as e:    print("UnicodeDecodeError")              # UnicodeDecodeErrorprint("café".encode("utf-8").decode("cp1252"))   # café -> silent garbage, even worse

A 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 with do under the hood?" — It calls __enter__ at the start and guarantees __exit__ at the end, which for files calls close().
  • "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() or pathlib?" — pathlib.Path is the modern way to build and inspect paths; its read_text/write_text are handy for small files, and path.open() works with with for streaming.