# Pastebin b1GrCEQ3 import gc import io # open('foo', 'wb').write(b'hello') print(open('foo', 'rb').read()) class Foo: def __del__(self): print('garbage collected') io.BytesIO(b'foo') Foo() gc.collect()