111. Python's io Module

The io module in Python provides the tools for working with various I/O operations such as reading from and writing to files, memory buffers, and other file-like objects. Below are 10 Python code snippets that demonstrate how to use the io module for efficient I/O operations:

1. Reading from a File with io

You can use the open() function from the io module to read from a file.

Copy

import io

# Open a file for reading
with io.open('example.txt', 'r', encoding='utf-8') as f:
    content = f.read()
    print(content)

Explanation:

  • io.open() is used to open a file in the specified mode (e.g., 'r' for reading) and encoding (e.g., 'utf-8').

  • It behaves similarly to the built-in open() function but with added flexibility for various types of I/O operations.


2. Writing to a File with io

To write data to a file, use the io.open() function with write mode.

Copy

import io

# Write to a file
with io.open('output.txt', 'w', encoding='utf-8') as f:
    f.write("Hello, World!")
    print("Data written to the file.")

Explanation:

  • io.open() is used with the 'w' mode to open the file for writing.

  • If the file doesn’t exist, it will be created.


3. Buffered I/O with io.BytesIO

The io.BytesIO class allows you to work with byte data in memory, simulating a file.

Copy

Explanation:

  • io.BytesIO() creates an in-memory buffer for bytes data, which can be treated as a file-like object.

  • You can write bytes and seek to different positions to read data.


4. Text I/O with io.StringIO

io.StringIO provides a way to work with strings as file-like objects.

Copy

Explanation:

  • io.StringIO() allows you to read and write strings in memory.

  • This is useful when you want to treat strings like files in I/O operations.


5. Reading Lines from a File

You can read lines from a file with io using a buffered approach.

Copy

Explanation:

  • Reading lines from a file can be done using a for loop, which automatically handles line breaks.


6. Writing Lines to a File

Write multiple lines to a file using io.

Copy

Explanation:

  • writelines() is used to write multiple lines to a file. Each line must include a newline character () if required.


7. Memory-Mapped File I/O with mmap

The mmap module can be used for memory-mapped file I/O, allowing you to map files directly into memory.

Copy

Explanation:

  • mmap() maps a file into memory, enabling fast and efficient access.

  • The fileno() function returns the file descriptor used by mmap().


8. Binary File Operations with io

For binary file reading and writing, you can use the rb or wb modes with io.open().

Copy

Explanation:

  • 'wb' mode is used for writing binary data to a file.

  • The b in the b'Hello' indicates that the data is in bytes.


9. Buffered File Reading and Writing

You can use io.BufferedReader and io.BufferedWriter for efficient I/O operations when dealing with large files.

Copy

Explanation:

  • io.BufferedReader() provides a buffer to read large files in chunks, improving performance for large datasets.


10. Creating Custom File-Like Objects

You can create your custom file-like objects by subclassing io.IOBase.

Copy

Explanation:

  • By subclassing io.IOBase, you can create your own file-like object with custom read, write, and other file operations.


Key Takeaways:

  • io.open(): Allows reading and writing files with encoding and various modes.

  • BytesIO: Works with bytes data in memory.

  • StringIO: Works with string data in memory.

  • BufferedReader / BufferedWriter: Efficient buffered I/O operations.

  • mmap: Provides memory-mapped file I/O for large files.

  • Binary and Text I/O: Use rb, wb, r, and w modes for binary and text files.

  • Custom File-Like Objects: Subclass io.IOBase to create custom file-like objects.

These operations are powerful tools in Python for handling I/O tasks, whether you're working with files, memory buffers, or custom data sources.

Last updated