Returning a custom file object (Python 3)

Ben Finney ben+python at benfinney.id.au
Wed May 27 22:34:49 EDT 2015


Steven D'Aprano <steve at pearwood.info> writes:

> from io import TextIOWrapper
> class MyFile(TextIOWrapper):
>     pass
>
> but how do I tell open() to use MyFile?

I haven't used it, but does the ‘opener’ parameter do what you want?

    open(file, mode='r', buffering=-1, encoding=None,
             errors=None, newline=None, closefd=True, opener=None) ->
             file object

    […]

    A custom opener can be used by passing a callable as *opener*. The
    underlying file descriptor for the file object is then obtained by
    calling *opener* with (*file*, *flags*). *opener* must return an
    open file descriptor (passing os.open as *opener* results in
    functionality similar to passing None).

-- 
 \          “Our products just aren't engineered for security.” —Brian |
  `\             Valentine, senior vice-president of Microsoft Windows |
_o__)                                                      development |
Ben Finney




More information about the Python-list mailing list