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

Skip Montanaro skip.montanaro at gmail.com
Tue Jan 16 11:00:16 EST 2018


I'd like to take advantage of the seekable() method of io.IOBase with
existing open file objects, especially the standard in/out/err file
objects. I know on Linux I can just do this:

    fp = io.open("/dev/stderr")

but that's kind of cheating. File objects in Python 2.7 aren't created
using the io module's machinery. Assume somewhere else a plain old
open file object, for example:

   fp = open("/some/path/to/nowhere.txt", "w")

and wanted to treat fp as if it had been opened with the io module
machinery, I don't see any sort of "fdopen" or "freopen" equivalent
mentioned in the io module documentation. Is this possible in a clean
way?

Thx,

Skip



More information about the Python-list mailing list