Intermediate·5 min·intermediate · io
In-memory I/O
In-memory I/O
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.