Python io Module

The io module in Python provides the main facilities for dealing with various types of I/O (input/output). This module provides Python's main facilities for dealing with binary and text I/O, including file I/O.

Table of Contents

  1. Introduction
  2. Key Classes and Functions
    • io.open
    • io.BytesIO
    • io.StringIO
    • io.TextIOWrapper
    • io.BufferedReader
    • io.BufferedWriter
    • io.BufferedRandom
  3. Examples
    • Reading and Writing Text Files
    • Reading and Writing Binary Files
    • Using BytesIO
    • Using StringIO
  4. Real-World Use Case
  5. Conclusion
  6. References

Introduction

The io module provides a consistent interface for the various types of I/O in Python. It supports text I/O, binary I/O, and raw I/O, making it used for handling file operations and stream handling.

Key Classes and Functions

io.open

Opens a file and returns a file object.

import io

with io.open('example.txt', 'w', encoding='utf-8') as file:
    file.write('Hello, World!')

io.BytesIO

Creates an in-memory binary stream.

import io

binary_stream = io.BytesIO()
binary_stream.write(b'Hello, World!')
print(binary_stream.getvalue())  # b'Hello, World!'

io.StringIO

Creates an in-memory text stream.

import io

text_stream = io.StringIO()
text_stream.write('Hello, World!')
print(text_stream.getvalue())  # 'Hello, World!'

io.TextIOWrapper

A buffered text stream over a binary stream.

import io

with open('example.bin', 'wb') as binary_file:
    with io.TextIOWrapper(binary_file, encoding='utf-8') as text_file:
        text_file.write('Hello, World!')

io.BufferedReader

A buffered binary stream reader.

import io

with open('example.bin', 'rb') as binary_file:
    buffered_reader = io.BufferedReader(binary_file)
    print(buffered_reader.read())

io.BufferedWriter

A buffered binary stream writer.

import io

with open('example.bin', 'wb') as binary_file:
    buffered_writer = io.BufferedWriter(binary_file)
    buffered_writer.write(b'Hello, World!')

io.BufferedRandom

A buffered binary stream for both reading and writing.

import io

with open('example.bin', 'w+b') as binary_file:
    buffered_random = io.BufferedRandom(binary_file)
    buffered_random.write(b'Hello, World!')
    buffered_random.seek(0)
    print(buffered_random.read())

Examples

Reading and Writing Text Files

import io

# Writing to a text file
with io.open('example.txt', 'w', encoding='utf-8') as file:
    file.write('Hello, World!')

# Reading from a text file
with io.open('example.txt', 'r', encoding='utf-8') as file:
    content = file.read()
    print(content)  # Hello, World!

Reading and Writing Binary Files

import io

# Writing to a binary file
with io.open('example.bin', 'wb') as file:
    file.write(b'Hello, World!')

# Reading from a binary file
with io.open('example.bin', 'rb') as file:
    content = file.read()
    print(content)  # b'Hello, World!'

Using BytesIO

import io

# Creating an in-memory binary stream
binary_stream = io.BytesIO()
binary_stream.write(b'Hello, World!')
print(binary_stream.getvalue())  # b'Hello, World!'

# Reading from the in-memory binary stream
binary_stream.seek(0)
print(binary_stream.read())  # b'Hello, World!'

Using StringIO

import io

# Creating an in-memory text stream
text_stream = io.StringIO()
text_stream.write('Hello, World!')
print(text_stream.getvalue())  # 'Hello, World!'

# Reading from the in-memory text stream
text_stream.seek(0)
print(text_stream.read())  # 'Hello, World!'

Real-World Use Case

Logging to an In-Memory Stream

In-memory streams can be useful for logging in applications where you want to keep the logs in memory for quick access.

import io
import logging

# Create an in-memory text stream
log_stream = io.StringIO()

# Configure logging to write to the in-memory stream
logging.basicConfig(stream=log_stream, level=logging.DEBUG)

# Log some messages
logging.debug('This is a debug message.')
logging.info('This is an info message.')

# Print the log contents
log_contents = log_stream.getvalue()
print(log_contents)

Conclusion

The io module in Python provides a powerful and flexible way to handle different types of I/O operations. It supports both text and binary I/O, and includes classes for in-memory streams, making it suitable for a wide range of applications.

References

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare