21  Context Manager

Python with statement (realpython)

from pathlib import Path

About cwd()

Path().cwd() # Check WD
PosixPath('/Users/kittipos/my_book/py-notes/basic/context')

21.1 Write file (Error Prone)

file = open("hello.txt", "w")
file.write("Hello, World!")
file.close()

To improve …

In Python, you can use two general approaches to deal with resource management. You can wrap your code in:

  • A try … finally construct (more general)

  • A with construct (more concise)

    • Works only objects that support the context management protocol

21.2 Write file (try ... finally)

# Safely open the file
file = open("hello.txt", "w")

try:
    file.write("Hello, World!")
except Exception as e:
    print(f"An error occurred while writing to the file: {e}")
finally:
    # Make sure to close the file after using it
    file.close()

21.3 Write file (with Statement)

21.3.1 Simple

with expression as target_var:
    do_something(target_var)

expression must return an object that implements the context management protocol. This protocol consists of two special methods:

  • .__enter__() is called by the with statement to enter the runtime context.
  • .__exit__() is called when the execution leaves the with code block.
with open("hello.txt", mode="w") as file:
    file.write("Hello, World!")

When you run this with statement, open() returns an io.TextIOBase object. This object is also a context manager, so the with statement calls .__enter__() and assigns its return value to file. Then you can manipulate the file inside the with code block. When the block ends, .__exit__() automatically gets called and closes the file for you, even if an exception is raised inside the with block.

21.3.2 Multiple

multiple context managers

with A() as a, B() as b:
    pass
with open("input.txt") as in_file, open("output.txt", "w") as out_file:
    # Read content from input.txt
    # Transform the content
    # Write the transformed content to output.txt
    pass

21.3.3 👍 Better (pathlib)

you should consider using Path.open() in your with statements as a best practice in Python.

from pathlib import Path

with Path("hello.txt").open("w") as file:
    file.write("Hello, World!")

In this example, you wrap the with statement in a try … except statement. If an OSError occurs during the execution of with, then you use logging to log the error with a user-friendly and descriptive message.

import pathlib
import logging

file_path = pathlib.Path("hellos.txt")

try:
    with file_path.open(mode="w") as file:
        file.write("Hello, World!")
except OSError as error:
    logging.error("Writing to file %s failed due to: %s", file_path, error)