[英]Get the path of the current TTY in Python
How do you get the device path for the current TTY? 如何获取当前TTY的设备路径?
Python has sys.stdin
, sys.stdout
and os.ttyname
but you cannot pass either of the formers to the latter because it requires a file descriptor. Python有
sys.stdin
, sys.stdout
和os.ttyname
但你不能将前任者传递给后者,因为它需要一个文件描述符。
You can pass sys.stdout.fileno
to os.ttyname
: 您可以将
sys.stdout.fileno
传递给os.ttyname
:
In [3]: os.ttyname(sys.stdout.fileno())
Out[3]: '/dev/pts/24'
This may have portability issues, but in most Unix-like environments /dev/stdout
refers to the current terminal and is unique to each connection [windows, tabs, ssh, etc.] you have open. 这可能有可移植性问题,但在大多数类似Unix的环境中,
/dev/stdout
指的是当前终端,并且对于您打开的每个连接[windows,tabs,ssh等]是唯一的。 Given those assumptions you can use: 鉴于您可以使用的假设:
with open('/dev/stdout') as fd:
tty_path = os.ttyname(fd.fileno())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.