How to obtain the file descriptor of the current terminal in python?

1.3k views Asked by At

sys.__stdout__.fileno() returns the file descriptor of the terminal window that this python terminal session begins in.

But if some bootstrap process closes this file or the file never existed to begin with, how would I create a new fd for the current terminal window so I can do something like:

sys.__stdout__ = os.fdopen(new_fd, 'w')
sys.__stdout__.write('hi\r\n')
'hi'
0

There are 0 answers