Python3 os.ttyname() Method
Python3 OS File/Directory Methods
Overview
The os.ttyname() method is used to return a string that represents the terminal device associated with file descriptor fd. If fd is not associated with a terminal device, an exception is raised.
Syntax
ttyname()The method syntax is as follows:
os.ttyname(fd)
Parameters
fd-- File descriptor
Return Value
Returns a string representing the terminal device associated with file descriptor fd.
Example
The following example demonstrates the use of the ttyname() method:
#!/usr/bin/python3
import os, sys
# 显示当前目录
print ("当前目录 :%s" %os.getcwd())
# 修改目录为 /dev/tty
fd = os.open("/dev/tty",os.O_RDONLY)
p = os.ttyname(fd)
print ("关联的终端为: ")
print (p)
print ("done!!")
os.close(fd)
print ("关闭文件成功!!")
The output of executing the above program is:
当前目录 :/tmp 关联的终端为: /dev/tty done!! 关闭文件成功!!Other Extensions