Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python file-like buffer object

Tags:

python

io

buffer

I've written a buffer class that provides a File-like interface with read, write, seek, tell, flush methods to a simple string in memory. Of course it is incomplete (e.g. I didn't write readline). 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?

like image 842
djs Avatar asked Sep 02 '09 15:09

djs


People also ask

How do you write a buffer file in Python?

Python flush() Python uses the operating system's default buffering unless you configure it do otherwise. Python automatically flushes the files when closing them. But you can also force flush the buffer to a file programmatically with the flush() method .

How do you create a file like an object in Python?

To create a file object in Python use the built-in functions, such as open() and os. popen() . IOError exception is raised when a file object is misused, or file operation fails for an I/O-related reason. For example, when you try to write to a file when a file is opened in read-only mode.

What is buffer in Python file?

Buffer structures (or simply “buffers”) are useful as a way to expose the binary data from another object to the Python programmer. They can also be used as a zero-copy slicing mechanism. Using their ability to reference a block of memory, it is possible to expose any data to the Python programmer quite easily.

What is BytesIO in Python?

StringIO and BytesIO are methods that manipulate string and bytes data in memory. StringIO is used for string data and BytesIO is used for binary data. This classes create file like object that operate on string data. The StringIO and BytesIO classes are most useful in scenarios where you need to mimic a normal file.


1 Answers

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.

like image 68
Kenan Banks Avatar answered Nov 11 '22 11:11

Kenan Banks