How to Use a Context Manager for File Operations

with open('example.txt', 'w') as file:
    file.write('Hello, this is an example.')

with open('example.txt', 'r') as file:
    content = file.read()
    print(f"Content of the file: {content}")

Output:

Content of the file: Hello, this is an example.

Leave a Comment

Your email address will not be published.