In-memory I/O
- Read and write files with
open()context managers - Handle text vs binary modes correctly
- Parse structured text like CSV without external libraries
- Understand encoding gotchas: UTF-8 vs ASCII
io.StringIO and io.BytesIO let code that expects a file object (pandas read_csv, csv.writer, PIL.Image.open) work with in-memory data — critical for unit tests and Lambda handlers where the filesystem is read-only or ephemeral.
- Forgetting to
seek(0)after writing — the read pointer is at the end; you'll get an empty string back. - Mixing
StringIO(text) with binary APIs likepickle.dump— useBytesIOfor anything non-text. - Not closing buffers in long-running processes — wrap in
with io.StringIO() as buf:to free memory.
Browser Python can't write to your disk — but it can write to memory with the same API.
io.StringIO / io.BytesIO
Behave just like a file object: .write(), .read(), .getvalue().
Why useful here
- Test code that expects a file handle
- Pipe between modules that all speak the file-object protocol (
csv,json,gzip)
Try it
- Write a JSON object to a
StringIOand read it back withjson.load. - Use
csv.DictReaderto parse the CSV above into a list of dicts.
3 graded exercises
Write the code, click Check. We’ll tell you exactly what to fix.
- Exercise 1
Create an
io.StringIObuffer, write"line one\nline two\n"to it, then read it back with.getvalue()and print. Expected output includes both lines. - Exercise 2
Iterate line-by-line over a StringIO seeded with
"a\nb\nc\n". Collect each stripped line into a listlinesand print it. Expected:['a', 'b', 'c']. - Exercise 3
Redirect stdout to a StringIO buffer while calling
print("captured"). Restore stdout, then print the captured contents. Expected output includescaptured.