Open In App

with statement in Python

Last Updated : 29 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The "with" statement in Python simplifies resource management by automatically handling setup and cleanup tasks. It's commonly used with files, network connections and databases to ensure resources are properly released even if errors occur making your code cleaner.

Why do we need "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.

Safe File Handling

When working with files, it’s important to open and close them properly to avoid issues like memory leaks or file corruption. Below are two simple examples using a file named example.txt that 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 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 file's content while automatically closing it, ensuring efficient resource management without a finally block.

Resource Management Using "with" Statement

Python’s with statement simplifies resource handling by managing setup and cleanup automatically. Let’s explore how it works and where it’s commonly used.

1. Using with statement for file handling

File handling is one of the most common use cases for 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.

2. Replacing Try-Except finally with "with" statement

Without "with" statement, 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.

3. Context Managers and "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.

4. Using 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.

5. Database Connection Management

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.

With Statement in Python
Article Tags :
Practice Tags :

Similar Reads

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy