file objects choices?

Stefan Schwarzer sschwarzer at sschwarzer.net
Mon Mar 17 16:26:04 EST 2003


Stefan Schwarzer wrote:
> There are also files generated from sockets. See the socket module.

See also StringIO (and cStringIO).

> Also some third-party modules/extensions provide file-like interfaces or even
> virtual file systems.

In fact, you don't depend on the Python core or the standard library. You can
build file-like objects with Python yourself: Code a class whose objects conform
to the same interface as regular files:

class MyFile:
     def read(self, length):
         ...
     def readlines(self, length):
         ...
     def write(self, string):
         ...

     ...

     def close(self):
         ...

my_file = MyFile()
my_file.write('An example\n')
my_file.close()

It's up to your creativity to invent useful file-like objects. ;-)

Stefan





More information about the Python-list mailing list