from pathlib import Path56 Context Manager
Python with statement (realpython)
About cwd()
- In Quarto or Jupyter it is the directory that .qmd or .ipynb in.
- In Python script it is the project root (where the terminal was run)
Path().cwd() # Check WDPosixPath('/Users/kittipos/my_book/py-notes/contents/basic/context')
56.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 … finallyconstruct (more general)A
withconstruct (more concise)- Works only objects that support the context management protocol
56.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()56.3 Write file (with Statement)
56.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.
56.3.2 Multiple
multiple context managers
with A() as a, B() as b:
passwith 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
pass56.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)