Computer >> Computer tutorials >  >> Programming >> Python

How to open a new pseudo-terminal pair using Python?


You can use the os.openpty() to open a new pseudo-terminal pair using Python. This method gives a pair of file descriptors (master, slave), for the master and the slave end, respectively.

Example

You can use it in the following way:

import os
# master for pty, slave for tty
m,s = os.openpty()
print m
print s
# showing terminal name
s = os.ttyname(s)
print m
print s

Output

You will get the output:

3
4
3
/dev/pty0