A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/with-statement-in-python/ below:

with statement in Python - GeeksforGeeks

with statement in Python

Last Updated : 11 Jul, 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 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:

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:

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:

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:



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4