🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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
- Introduction
- Key Classes and Functions
io.openio.BytesIOio.StringIOio.TextIOWrapperio.BufferedReaderio.BufferedWriterio.BufferedRandom
- Examples
- Reading and Writing Text Files
- Reading and Writing Binary Files
- Using
BytesIO - Using
StringIO
- Real-World Use Case
- Conclusion
- 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
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment