當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。