当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python os.ttyname()用法及代码示例


Python中的OS模块提供了与操作系统进行交互的函数。操作系统属于Python的标准实用程序模块。该模块提供了使用依赖于操作系统的函数的便携式方法。

os.ttyname()Python中的方法用于获取与指定文件描述符关联的终端设备。

文件描述符是小整数值,与文件或其他输入/输出资源(例如管道或网络套接字)相对应。它是资源的抽象指示器,并充当执行各种较低级别I /O操作(如读取,写入,发送等)的句柄。
例如:标准输入通常是值为0的文件描述符,标准输出通常是值为1的文件描述符,标准错误通常是值为2的文件描述符。这些文件描述符即0(stdin),1(stdout)和2(stderr)为我们的程序使用的标准文件描述符。当前进程打开的其他文件将获得值3、4、5,依此类推。


注意:此方法仅在UNIX平台上可用,并且如果指定的文件描述符未与任何终端设备关联,则会引发异常。

用法: os.ttyname(fd) 

参数:
fd:文件描述符

返回类型:此方法返回一个字符串值,该值表示与指定文件描述符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


相关用法


注:本文由纯净天空筛选整理自ihritik大神的英文原创作品 Python | os.ttyname() method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。