with statement in Python (original) (raw)
Last Updated : 03 Mar, 2025
The with statement in Python is used for resource management and exception handling. It simplifies working with resources like files, network connections and database connections by ensuring they are properly acquired and released. When we open a file, we need to close it ourself using close(). But if something goes wrong before closing, the file might stay open, causing issues. Using **with open() automatically closes the file when we’re done, even if an error happens.
**example.txt file **contains:
Hello, World!
**Example 1 : Without with (Manual closing)
Python `
file = open("example.txt", "r") try: content = file.read() print(content) finally: file.close() # Ensures the file is closed
`
**Output
Hello, World!
**Explanation: This code opens **“example.txt” in read mode, reads its content, prints it and ensures the file is closed using a finally block.
**Example 2: using with(Automatic closing)
Python `
with open("example.txt", "r") as file: content = file.read() print(content) # File closes automatically
`
**Output
Hello, World!
**Explanation: with open(…) statement reads and prints the file’s content while automatically closing it, ensuring efficient resource management without a finally block.
Advantages of the with statement
- **Simplifies Resource Management : with statement ensures that resources are properly acquired and released, reducing the likelihood of resource leaks.
- **Replaces Try-Except-Finally Blocks: Traditionally, resource management required try-except-finally blocks to handle exceptions and ensure proper cleanup. The with statement provides a more concise alternative.
- **Enhances Readability: By reducing boilerplate code, the with statement improves code readability and maintainability.
Using the with statement for file handling
File handling is one of the most common use cases for the with statement. When opening files using open(), the with statement ensures that the file is closed automatically after operations are completed.
**Example 1 : Reading a file
Python `
with open("example.txt", "r") as file: contents = file.read() print(contents) # Print file content
`
**Output:
Hello, World!
**Explanation: Opens example.txt in read mode (“r”) and with ensures automatic file closure after reading and file.read() reads the entire file content into contents.
**Example 2 : Writing to a file
Python `
with open("example.txt", "w") as file: file.write("Hello, Python with statement!")
`
**Output:
Hello, Python with statement!
**Explanation: The file is opened in write mode (“w”). After the with block, the file is automatically closed.
Replacing Try-Except finally with statement
Without with, you need to explicitly manage resource closure:
**Example 1 : Without using with
Python `
file = open("example.txt", "w") try: file.write("Hello, Python!") finally: file.close() # Ensure file is closed
`
**Output
Hello, World!
**Explanation: This code opens example.txt in write mode (“w”), creating or clearing it. The **try block writes “Hello, Python!” and **finally ensures the file closes, preventing resource leaks.
**Example 2: using with
Python `
with open("example.txt", "w") as file: file.write("Hello, Python!")
`
**Output
Hello, Python!
**Explanation: This code opens **example.txt in write mode (“w”) using with, which ensures automatic file closure. It writes “Hello, Python!” to the file, replacing any existing content.
Context Managers and the with statement
The with statement relies on context managers, which manage resource allocation and deallocation using two special methods:
- __enter__(): Acquires the resource and returns it.
- __exit__(): Releases the resource when the block exits
**Example: Custom context manager for file writing
Python `
class FileManager: def init(self, filename, mode): self.filename = filename self.mode = mode
def __enter__(self):
self.file = open(self.filename, self.mode)
return self.file
def __exit__(self, exc_type, exc_value, traceback):
self.file.close()
using the custom context manager
with FileManager('example.txt', 'w') as file: file.write('Hello, World!')
`
**Output:
Hello, World!
**Explanation:
- __init__() initializes the filename and mode, __enter__() opens the file, and __exit__() ensures it closes automatically.
- with FileManager(‘file.txt’, ‘w’) as file: opens “file.txt” in write mode.
- file.write(‘Hello, World!’) writes to the file, which closes upon exiting the block.
Using the contextlib Module
Instead of creating a full class, Python provides the contextlib module to create context managers using functions.
**Example: Function-Based Context Manager
Python `
from contextlib import contextmanager
@contextmanager def open_file(filename, mode): file = open(filename, mode) try: yield file finally: file.close()
Using the generator-based context manager
with open_file('example.txt', 'w') as file: file.write('Hello, World!')
`
**Output :
Hello, World!
**Explanation:
- ****@contextmanager,** where open_file() opens a file and yields it for use.
- Ensures automatic file closure with a finally block, even if an exception occurs.
- Writes “Hello, World!” to “file.txt” and the file closes automatically after the with block.
Other Use Cases of with Statement
The with statement is not limited to file handling. It is widely used in managing database connections, for example:
Python `
import sqlite3
with sqlite3.connect("example.db") as conn:
cursor = conn.cursor()
cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='users'")
res = cursor.fetchone()
print("Table created successfully!" if res else "Table not found."
`
**Example Output
if the users table exits:
Table created successfully!
If the users table does not exits:
Table not found.
**Explanation:
- Connects to example.db, auto-closes after execution and **conn.cursor() creates a cursor for SQL execution.
- **cursor.execute(…) checks if the “users” table exists and **cursor.fetchone() gets result None if table not found.