#!/usr/bin/env python3 import tempfile, io smiley = '\u263a' # A Unicode smiley character spool = tempfile.SpooledTemporaryFile(max_size=5, mode='w+', encoding='utf-8') spool.write(smiley*10) # Gets written to io.StringIO, then forces rollover to TemporaryFile # Uncomment the following to circumvent the bug #spool.seek(0, io.SEEK_END) # Force current position to end of TemporaryFile spool.write(smiley*10) # Overlays some previous bytes because of bug spool.seek(0) # Prepare to read back what we wrote data = spool.read() print('We read', data) if data == smiley*20: print('It matches what we wrote') else: print('It doesn\'t match what we wrote')