Sobes.tech
Junior — Middle

Which operator in Python provides automatic resource management using context managers?

sobes.tech AI

Answer from AI

In Python, the with statement provides automatic resource management using context managers. It ensures that necessary actions for opening and closing resources (such as files, network connections) are performed correctly, even if exceptions occur within the block of code.

Example usage:

with open('file.txt', 'r') as file:
    data = file.read()
# The file is automatically closed after exiting the with block