Python| os.ttyname() 方法
Python中的OS 模块提供了与操作系统交互的功能。操作系统属于 Python 的标准实用程序模块。该模块提供了一种使用操作系统相关功能的可移植方式。
Python中的os.ttyname()
方法用于获取与指定文件描述符关联的终端设备。
文件描述符是对应于文件或其他输入/输出资源(例如管道或网络套接字)的小整数值。它是资源的抽象指标,并充当句柄来执行各种较低级别的 I/O 操作,如读、写、发送等。
例如:标准输入通常是值为 0 的文件描述符,标准输出通常是值为 1 的文件描述符,标准错误通常是值为 2 的文件描述符。这些文件描述符即 0 (stdin)、1 (stdout) 和 2 ( stderr) 是我们的程序使用的标准文件描述符。当前进程打开的其他文件将获得值 3、4、5 等等。
注意:此方法仅在 UNIX 平台上可用,如果指定的文件描述符未与任何终端设备关联,则会引发异常。
Syntax: os.ttyname(fd)
Parameter:
fd: A file descriptor
Return Type: This method returns a string value which represents the terminal device associated with the specified file descriptor fd.
代码 #1:使用 os.ttyname() 方法获取与文件描述符关联的终端设备。
# Python program to explain os.ttyname() method
# importing os module
import os
# Standard file descriptors
# i.e 0 (stdin), 1 (stdout), and 2 (stderr)
# are used by our program
# Get the terminal device
# associated with these file descriptor
print("Terminal device associated with:")
print("Standard input:", os.ttyname(0))
print("Standard output:", os.ttyname(1))
print("standard error", os.ttyname(2))
# Open a new pseudo-terminal pair
# using os.openpty() method
# It will return master and slave
# file descriptor for
# pty ( pseudo terminal device) and
# tty ( native terminal device) respectively
master, slave = os.openpty()
# Get the terminal device
# associated with these file descriptor
print("Master:", os.ttyname(master))
print("Slave:", os.ttyname(slave))
输出:
Terminal device associated with:
Standard input: /dev/pts/0
Standard output: /dev/pts/0
standard error /dev/pts/0
Master: /dev/ptmx
Slave: /dev/pts/1
代码#2:如果指定的文件描述符没有与任何终端设备关联
# Python program to explain os.ttyname() method
# importing os module
import os
# If the specified file descriptor
# is not associated with any
# terminal device, then an exception
# will be raised. For example:
# Create a pipe using os.pipe() method
# It will return a pair of
# file descriptors (r, w) usable for
# reading and writing, respectively.
r, w = os.pipe()
# Get the terminal device associated
# with the file descriptor r or w
print(os.ttyname(r))
输出:
Traceback (most recent call last):
File "getTerminalDevice.py", line 20, in
print(os.ttyname(r))
OSError: [Errno 25] Inappropriate ioctl for device
代码#3:处理上述异常的方法
# Python program to explain os.ttyname() method
# importing os module
import os
# Create a pipe using os.pipe() method
# It will return a pair of
# file descriptors (r, w) usable for
# reading and writing, respectively.
r, w = os.pipe()
# Method 1
# (using exception handling technique)
# Try to get the terminal device associated
# with the file descriptor r or w
try :
print(os.ttyname(r))
except OSError as error :
print(error)
print("File descriptor is not associated with any terminal device")
# Method 2
# using os.isatty() method
# check first if the file descriptor w
# is associated with a tty(-like) device or not
# if it is then only get the terminal
# device associated with it
if os.isatty(w) :
print(os.ttyname(w))
else :
print("File descriptor is not associated with any terminal device")
输出:
[Errno 25] Inappropriate ioctl for device
File descriptor is not associated with any terminal device
File descriptor is not associated with any terminal device