🎓 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 mmap module in Python provides memory-mapped file support, allowing a file's contents to be mapped directly into memory. This can lead to efficient file I/O operations and is particularly useful for accessing parts of a file without reading the entire file into memory.
Table of Contents
- Introduction
- Key Classes and Methods
mmap.mmapmmap.closemmap.findmmap.flushmmap.readmmap.readlinemmap.resizemmap.seekmmap.sizemmap.writemmap.write_byte
- Examples
- Creating a Memory-Mapped File
- Reading from a Memory-Mapped File
- Writing to a Memory-Mapped File
- Searching within a Memory-Mapped File
- Resizing a Memory-Mapped File
- Real-World Use Case
- Conclusion
- References
Introduction
Memory mapping a file is a way to create a direct byte-for-byte mapping of a file into memory. This can significantly improve file I/O performance by allowing programs to access file data as if it were in-memory data. The mmap module provides a simple interface for memory-mapping files in Python.
Key Classes and Methods
mmap.mmap
Creates a memory-mapped file object.
import mmap
# Open a file for reading and writing
with open('example.txt', 'r+b') as f:
mm = mmap.mmap(f.fileno(), 0)
mmap.close
Closes the memory-mapped file.
mm.close()
mmap.find
Searches for a substring in the memory-mapped file.
index = mm.find(b'substring')
mmap.flush
Flushes changes made to the memory-mapped file to the disk.
mm.flush()
mmap.read
Reads data from the memory-mapped file.
data = mm.read(size)
mmap.readline
Reads a line from the memory-mapped file.
line = mm.readline()
mmap.resize
Resizes the memory-mapped file.
mm.resize(new_size)
mmap.seek
Moves the file pointer to a new position.
mm.seek(position)
mmap.size
Returns the size of the memory-mapped file.
size = mm.size()
mmap.write
Writes data to the memory-mapped file.
mm.write(b'data')
mmap.write_byte
Writes a single byte to the memory-mapped file.
mm.write_byte(b'X')
Examples
Creating a Memory-Mapped File
import mmap
# Open a file for reading and writing
with open('example.txt', 'r+b') as f:
mm = mmap.mmap(f.fileno(), 0)
# Remember to close the memory-mapped file
mm.close()
Reading from a Memory-Mapped File
import mmap
# Open a file for reading
with open('example.txt', 'r+b') as f:
mm = mmap.mmap(f.fileno(), 0)
# Read the first 10 bytes
print(mm.read(10))
# Move the file pointer to the beginning
mm.seek(0)
# Read the first line
print(mm.readline())
mm.close()
Writing to a Memory-Mapped File
import mmap
# Open a file for reading and writing
with open('example.txt', 'r+b') as f:
mm = mmap.mmap(f.fileno(), 0)
# Write data to the file
mm.write(b'Hello, World!')
# Flush changes to the disk
mm.flush()
mm.close()
Searching within a Memory-Mapped File
import mmap
# Open a file for reading
with open('example.txt', 'r+b') as f:
mm = mmap.mmap(f.fileno(), 0)
# Find a substring
index = mm.find(b'World')
if index != -1:
print(f'Substring found at index {index}')
else:
print('Substring not found')
mm.close()
Resizing a Memory-Mapped File
import mmap
# Open a file for reading and writing
with open('example.txt', 'r+b') as f:
mm = mmap.mmap(f.fileno(), 0)
# Resize the file
mm.resize(100)
mm.close()
Real-World Use Case
Efficient Log File Processing
When processing large log files, it can be inefficient to load the entire file into memory. Using memory-mapped files allows you to access and process the file in chunks.
import mmap
def process_log(file_path):
with open(file_path, 'r+b') as f:
mm = mmap.mmap(f.fileno(), 0)
while True:
line = mm.readline()
if not line:
break
process_line(line)
mm.close()
def process_line(line):
# Implement your log processing logic here
print(line)
if __name__ == '__main__':
process_log('log.txt')
Conclusion
The mmap module in Python provides a powerful way to work with files by mapping their contents directly into memory. This can lead to significant performance improvements for I/O-intensive applications. By using memory-mapped files, you can efficiently read, write, and search within large files without loading the entire file into memory.
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