Skip to main content
The io module provides Python’s main facilities for dealing with various types of I/O.

Module Import

import io

Text I/O

StringIO - In-Memory Text Stream

import io

# Write to string buffer
output = io.StringIO()
output.write('Hello, ')
output.write('World!')
print(output.getvalue())  # 'Hello, World!'
output.close()

# Read from string buffer
input_stream = io.StringIO('Line 1\nLine 2\nLine 3')
for line in input_stream:
    print(line.strip())

Binary I/O

BytesIO - In-Memory Binary Stream

import io

# Write binary data
output = io.BytesIO()
output.write(b'Hello, ')
output.write(b'World!')
print(output.getvalue())  # b'Hello, World!'
output.close()

# Read binary data
input_stream = io.BytesIO(b'\x00\x01\x02\x03')
data = input_stream.read(2)
print(data)  # b'\x00\x01'

File Objects

import io

# Open file in text mode
with io.open('file.txt', 'r', encoding='utf-8') as f:
    content = f.read()

# Open file in binary mode
with io.open('file.bin', 'rb') as f:
    data = f.read()

Buffered I/O

import io

# Buffered reader
with io.open('large_file.txt', 'rb') as f:
    buffered = io.BufferedReader(f)
    chunk = buffered.read(1024)  # Read 1KB

# Buffered writer
with io.open('output.txt', 'wb') as f:
    buffered = io.BufferedWriter(f)
    buffered.write(b'Data to write')
    buffered.flush()

Practical Examples

Capture Print Output

import io
import sys

# Capture stdout
old_stdout = sys.stdout
sys.stdout = io.StringIO()

print("This goes to StringIO")
print("Another line")

output = sys.stdout.getvalue()
sys.stdout = old_stdout

print(f"Captured: {output}")

Create File-Like Object from String

import io
import csv

# Use StringIO with csv module
csv_data = "name,age\nAlice,30\nBob,25"
string_io = io.StringIO(csv_data)

reader = csv.DictReader(string_io)
for row in reader:
    print(row)

os

Operating system interfaces

pathlib

Object-oriented filesystem paths

Build docs developers (and LLMs) love