Python os.ttyname()方法

2019-10-16 23:05:16

Python的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!!