Right way to io.open(...) an existing file object in Python 2.7?
eryk sun
eryksun at gmail.com
Tue Jan 16 14:40:23 EST 2018
On Tue, Jan 16, 2018 at 4:00 PM, Skip Montanaro
<skip.montanaro at gmail.com> wrote:
> 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.
io.open can open a file descriptor. If you don't use a duplicated FD
(os.dup), then you probably also want the option `closefd=False`. For
example:
$ cat test.py
import sys
import io
stdin = io.open(sys.stdin.fileno(), closefd=False)
print 'stdin seekable: %s' % stdin.seekable()
$ python test.py
stdin seekable: False
$ echo spam | python test.py
stdin seekable: False
$ python test.py < test.py
stdin seekable: True
More information about the Python-list
mailing list