djs djs - 1 year ago 224
Python Question

python file-like buffer object

I've written a buffer class that provides a File-like interface with

methods to a simple string in memory. Of course it is incomplete (e.g. I didn't write
). It's purpose is to be filled by a background thread from some external data source, but let a user treat it like a file. I'd expect it to contain a relatively small amount of data (maybe 50K max)

Is there a better way to do this instead of writing it from scratch?

Answer Source

You can use the standard Python modules StringIO or cStringIO to obtain an in-memory buffer which implements the file interface.

cStringIO is implemented in C, and will be faster, so you should use that version if possible.

If you're using Python 3 you should use the io.StringIO instead of StringIO and io.BytesIO instead of cStringIO.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download