Python os.ttyname() 方法



Python 的 os.ttyname() 方法用於返回一個字串,該字串表示與傳遞給它的檔案描述符關聯的終端裝置。如果檔案描述符與終端裝置沒有關聯,則此方法將引發異常。

語法

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

os.ttyname(fd)

引數

Python 的 os.ttyname() 僅接受一個引數:

  • fd - 此引數指定檔案描述符。

返回值

Python 的 os.ttyname() 方法返回一個字串,該字串指定終端裝置。

示例

在以下示例中,我們使用 os.ttyname() 方法檢查與給定檔案描述符關聯的終端裝置。

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 ("file closed successfully!!")

當我們執行以上程式時,它會產生以下結果:

Current working dir :/home/tp/Python
the terminal device associated is: 
/dev/tty
done!!
file closed successfully!!
python_files_io.htm
廣告