Python3 os.ttyname() Method
Python3 OS File/Directory Methods
Overview
The os.ttyname() method returns a string representing the terminal device associated with the file descriptor fd. If fd is not associated with a terminal device, an exception is raised.
Syntax
The **ttyname()** method syntax is as follows:
os.ttyname(fd)
Parameters
-
fd -- The file descriptor
Return Value
Returns a string representing the terminal device associated with the file descriptor fd.
Example
The following example demonstrates the use of the ttyname() method:
#!/usr/bin/python3
import os, sys
# Show current directory
print ("当前目录 :%s" %os.getcwd())
# Change directory to /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 the above program is:
当前目录 :/tmp
The associated terminal is:
/dev/tty
done!!
关闭文件成功!!