Right way to io.open(...) an existing file object in Python 2.7?

Skip Montanaro skip.montanaro at gmail.com
Tue Jan 16 13:08:29 EST 2018


> If it's difficult to imagine a circumstance in which you would want to
> seek on stdio/out/err where you were not making some considerable error.
> But they are already io objects so you can just call sys.stdin.seekable
> or whatever.

Alas, in Python 2.7 sys.stdin/stdout/stderr are traditional (legacy?)
file objects created using the open() built-in function. I was hoping
there would be a clean way to transmogrify such objects into objects
which support the io.IOBase APIs. I already know that I can attempt to
seek(), then catch the IOError, but I was hoping to start using the
more modern io module's seekable() API in a specific module I maintain
without having to worry about what type of file-ish object I was
passed. Data flow might look like this:

File or io.IOBase object -> passed into my code -> transmogrified into
io.IOBase object at the boundary, if necessary -> resulting in io
nirvana in the internals of my module. :-)

Skip



More information about the Python-list mailing list