Python3 os.ttyname()方法

ttyname() 方法返回一個字串,它指定與fd有關終端設備。如果 fd 沒有與終端設備相關聯,將引發異常。

語法

以下是 ttyname() 方法的語法:
os.ttyname(fd)

參數

  • fd -- 這是在檔描述符

返回值

該方法返回一個字串,它指定終端設備。可在類 Unix 系統

示例

下麵的示例顯示 ttyname() 方法的使用。
# !/usr/bin/python33

import os, sys

# Showing current directory
print ("Current working dir :%s" %os.getcwd())

# Changing dir to /dev/tty
fd = os.open("/dev/tty",os.O_RDONLY)

p = os.ttyname(fd)
print ("the terminal device associated is: ")
print p
print ("done!!")

os.close(fd)
print ("Closed the file successfully!!")
當我們運行上面的程式,它會產生以下結果:
Current working dir is :/tmp
the terminal device associated is:
/dev/tty
done!!
Closed the file successfully!!

上一篇: Python3檔方法 下一篇: Python3 os檔目錄的方法